Basic Haskell: data types and classes

 

Functional Programming in Haskell
3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: data types and classes
bullet Lists as defined data types
As mentioned above, lists are pre-defined in Haskell; but they behave exactly as if defined as a recursive algebraic data type
data List a = Nil | Cons a (List a)

[1,2,3]  ==  1 : (2 : (3 : []))  ==  Cons 1 (Cons 2 (Cons 3 Nil))

(note that the constructors Nil and Cons are spelled differently in actual Haskell code)

Control bar


















































 

Functional Programming in Haskell
3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: data types and classes
bullet Lists as defined data types
bullet The role of data constructors
Data constructors such as Nil and Cons for lists are applied like functions, but they are really only "place-holders" which build larger structures from smaller ones

Control bar


















































 

Functional Programming in Haskell
3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: data types and classes
bullet Lists as defined data types
bullet The role of data constructors
bullet Function definition by pattern matching
We can "de-construct" constructed data either with pre-defined functions (such as head and tail) or by using pattern-matching, a kind of "definition by example"
length Nil = 0
length (Cons x y) = 1 + length y

Control bar


















































 

Functional Programming in Haskell
3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: data types and classes
bullet Lists as defined data types
bullet The role of data constructors
bullet Function definition by pattern matching
bullet Other data type definitions
We can define our own data types in Haskell and use their constructors to build data structures or in pattern-matching to define functions on these structures
data BTree a = Tip | Node a (BTree a) (BTree a)

ins x  Tip = Node x Tip Tip
ins x (Node y l r) | x <= y    = Node y (ins x l) r
                   | otherwise = Node y l (ins x r)

Control bar


















































 

Functional Programming in Haskell
3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: data types and classes
bullet Lists as defined data types
bullet The role of data constructors
bullet Function definition by pattern matching
bullet Other data type definitions
bullet Higher-order, nested and other complex types
Haskell has a rich type system which supports powerful data structuring techniques: data types may be "higher-order" (viewed as functions from types to types) and recursion may be nested
data GenTree s a = Branch a (s (GenTree s a))

data PerfTree a = Empty | Split a (PerfTree (a,a))

Control bar


















































 

Functional Programming in Haskell
3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: data types and classes
bullet Lists as defined data types
bullet The role of data constructors
bullet Function definition by pattern matching
bullet Other data type definitions
bullet Higher-order, nested and other complex types
bullet Class system allows for O-O style code sharing
In addition to powerful data types, Haskell allows types to be classified into groups based on the availability of functions matching a particular interface specification (as in many O-O languages)
class Printable a where
  print :: a -> String

instance Printable Foo where
  print x = ...

Control bar