chapter3 - PROGRAMMING IN HASKELL Chapter3TypesandClasses 1...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 What is a Type? A type  is a name for a collection of related values.   For example, in Haskell the basic type True False Bool contains the two logical values:
Background image of page 2
3 Type Errors Applying a function to one or more arguments of the  wrong type is called a type error . > 1 + False Error 1 is a number and False is a logical  value, but + requires two numbers.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Types in Haskell If evaluating an expression e would produce a  value of type t, then e has type  t, written e :: t Every well formed expression has a type, which  can be automatically calculated at compile time  using a process called type inference .
Background image of page 4
5 All type errors are found at compile time, which  makes programs safer and faster  by removing  the need for type checks at run time. In Hugs, the :type  command calculates the type  of an expression, without evaluating it: > not False True > :type not False not False :: Bool
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Basic Types Haskell has a number of basic types , including: Bool -  logical values Char -  single characters Integer -  arbitrary-precision integers Float -  floating-point numbers String -  strings of characters Int -  fixed-precision integers
Background image of page 6
7 List Types [False,True,False] :: [Bool] [’a’,’b’,’c’,’d’]  :: [Char] In general: A list  is sequence of values of the same  type: [t] is the type of lists with elements of type t.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 The type of a list says nothing about its length: [False,True]       :: [Bool] [False,True,False] :: [Bool] [[’a’],[’b’,’c’]] :: [[Char]] Note: The type of the elements is unrestricted.  For  example, we can have lists of lists:
Background image of page 8
9 Tuple Types
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

chapter3 - PROGRAMMING IN HASKELL Chapter3TypesandClasses 1...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online