3rd CCSC Northwest Conference • Fall 2001

 Currying Haskell functions of several arguments are actually "curried", i.e., they are higher-order functions of successive arguments ```> :t take take :: Int -> [a] -> [a] > :t take 10 take 10 :: [a] -> [a] > :t take 10 ['a'..'z'] take 10 (enumFromTo 'a' 'z') :: [Char]```

```

```

 3rd CCSC Northwest Conference • Fall 2001

 Currying Infix operators versus prefix functions Infix operators are just curried functions; by default, symbolic identifiers are written infix and alphabetic ones prefix, but we can over-ride these defaults ```> :t (+) (+) :: Num a => a -> a -> a > (+) 10 20 30 > 10 `divMod` 3 (3,1)```

```

```

 3rd CCSC Northwest Conference • Fall 2001

 Currying Infix operators versus prefix functions Operator sections We can conveniently apply an infix operator to just one argument by enclosing the phrase in parentheses ```> (2^) 10 1024 > (^2) 10 100```

```

```

 3rd CCSC Northwest Conference • Fall 2001

 Currying Infix operators versus prefix functions Operator sections 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"] > reverse ["hey", "there", "world"] ["world","there","hey"]```

```

```

 3rd CCSC Northwest Conference • Fall 2001

 Currying Infix operators versus prefix functions Operator sections The map functional Higher-order predicates Predicates (boolean-valued functions) can be extended to lists via the higher-order predicates any and all ```> map even [1..5] [False,True,False,True,False] > all even (map (2*) [1..5]) True > any odd [ x^2 | x<-[1..5] ] True```

```

```

 3rd CCSC Northwest Conference • Fall 2001

 Currying Infix operators versus prefix functions Operator sections The map functional Higher-order predicates 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 > foldl (&&) True (map even [2,4..10]) True```

```

```

 3rd CCSC Northwest Conference • Fall 2001

 Currying Infix operators versus prefix functions Operator sections The map functional Higher-order predicates The fold functions Other useful higher-order functions The standard Prelude defines scores of useful functions, many of which enjoy great generality due to the abstractional capabilities of polymorphic types and higher-order functions ```> zipWith (*) [1..10] [1..10] [1,4,9,16,25,36,49,64,81,100] > :t replicate replicate :: Int -> a -> [a] > zipWith replicate [1..6] ['a'..'z'] ["a","bb","ccc","dddd","eeeee","ffffff"] > takeWhile (<100) [ 2^n | n<-[1..] ] [2,4,8,16,32,64] > :t takeWhile takeWhile :: (a -> Bool) -> [a] -> [a]```

```

```