A Quick Taste of Haskell (II)

 Willamette Mathematics Colloquium • Fall 2002

A Quick Taste of Haskell (II)
 Booleans and if expressions The usual boolean values are written with initial caps; the conditional construct is an expression, not a statement ```> if 3 < 2 then "oops" else "hurray!" "hurray!" > (if 3 < 2 then (+) else (*)) 10 20 200```

```

```

 Willamette Mathematics Colloquium • Fall 2002

A Quick Taste of Haskell (II)
 Booleans and if expressions Infinite lists and lazy evaluation Haskell provides lazy evaluation for functions and data structures, so we can define infinite lists (it is prudent to avoid evaluation of a whole infinite list!) ```> [1,3..] [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,{Interrupted!} > take 20 [1,3..] [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39]```

```

```

 Willamette Mathematics Colloquium • Fall 2002

A Quick Taste of Haskell (II)
 Booleans and if expressions Infinite lists and lazy evaluation Higher-order functions Haskell allows functions to take other functions as arguments, or to return functions as results ```> even 3 False > any even [1,3..11] False > any (>10) [1..] True```

```

```

 Willamette Mathematics Colloquium • Fall 2002

A Quick Taste of Haskell (II)
 Booleans and if expressions Infinite lists and lazy evaluation Higher-order functions Currying Haskell functions of several arguments are actually "curried", i.e., they are higher-order functions of successive arguments ```> 2 + 3 5 > (+) 2 3 5 > :t (+) (+) :: Num a => a -> a -> a > :t (2+) (2 +) :: Num a => a -> a```

```

```

 Willamette Mathematics Colloquium • Fall 2002

A Quick Taste of Haskell (II)
 Booleans and if expressions Infinite lists and lazy evaluation Higher-order functions Currying The map functional The map functional takes a function as its first argument, then applies it to every element of a list ```> map (^2) [1..10] [1,4,9,16,25,36,49,64,81,100] > map (`div` 3) [1..20] [0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6] > map reverse ["hey", "there", "world"] ["yeh","ereht","dlrow"]```

```

```

 Willamette Mathematics Colloquium • Fall 2002

A Quick Taste of Haskell (II)
 Booleans and if expressions Infinite lists and lazy evaluation Higher-order functions Currying The map functional The fold functions The fold functions foldl and foldr combine elements of a list based on a binary function and an initial value ```> foldr (+) 0 [1..10] 55 > sum [1..10] 55 > foldr (*) 1 [1..5] == 1 * 2 * 3 * 4 * 5 * 1 True > foldr (++) [] ["a", "bb", "ccc"] "abbccc"```

```

```

 Willamette Mathematics Colloquium • Fall 2002

A Quick Taste of Haskell (II)
 Booleans and if expressions Infinite lists and lazy evaluation Higher-order functions Currying The map functional The fold functions Strong, static typing expressions in Haskell are only well-formed if they are well-typed according to a sophisticated type system ```> :t ['a'..'z'] enumFromTo 'a' 'z' :: [Char] > :t reverse reverse :: [a] -> [a] > :t foldr foldr :: (a -> b -> b) -> b -> [a] -> b > :t foldr (++) [] foldr (++) [] :: [[a]] -> [a]```

```

```

 Willamette Mathematics Colloquium • Fall 2002

A Quick Taste of Haskell (II)
 Booleans and if expressions Infinite lists and lazy evaluation Higher-order functions Currying The map functional The fold functions Strong, static typing More examples of Haskell in actions here are some links to external files defining Haskell types and functions • • • • •

```

```