Chapter10 - PROGRAMMING IN HASKELL 1 Type Declarations InHaskell, typeString=[Char [Char 2 easiertorea

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 10 - Declaring 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 Type Declarations In Haskell, a new name for an existing type can be  defined using a type declaration . type String = [Char] String is a synonym for the type [Char].
Background image of page 2
3 Type declarations can be used to make other types  easier to read.  For example, given origin    :: Pos origin     = (0,0) left      :: Pos   Pos left (x,y) = (x-1,y) type Pos = (Int,Int) we can define:
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 Like function definitions, type declarations can also  have parameters .  For example, given type Pair a = (a,a) we can define: mult      :: Pair Int   Int mult (m,n) = m*n copy      :: a   Pair a copy x     = (x,x)
Background image of page 4
5 Type declarations can be nested: type Pos   = (Int,Int) type Trans = Pos   Pos However, they cannot be recursive: type Tree = (Int,[Tree])
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 Data Declarations A completely new type can be defined by specifying  its values using a data declaration . data Bool = False | True Bool is a new type, with two  new values False and True.
Background image of page 6
7 Note: The two values False and True are called the  constructors  for the type Bool. Type and constructor names must begin with  an upper-case letter. Data declarations are similar to context free  grammars.  The former specifies the values of  a type, the latter the sentences of a language.
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 answers     :: [Answer] answers      = [Yes,No,Unknown] flip        :: Answer   Answer flip Yes     = No flip No      = Yes flip Unknown = Unknown data Answer = Yes | No | Unknown we can define: Values of new types can be used in the same ways  as those of built in types.  For example, given 
Background image of page 8
9 The constructors in a data declaration can also have  parameters.  For example, given data Shape = Circle Float            | Rect Float Float square         :: Float 
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.

This note was uploaded on 06/11/2011 for the course CSCE 330 taught by Professor Valtorta during the Spring '10 term at Columbia SC.

Page1 / 27

Chapter10 - PROGRAMMING IN HASKELL 1 Type Declarations InHaskell, typeString=[Char [Char 2 easiertorea

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