Course map
Haskell // 15

Higher-order functions

Read

Functions as values

Functions are values like any other: pass them as arguments, return them, keep them in lists. map and filter are higher-order functions; so is foldr.

twice :: (a -> a) -> a -> a
twice f x = f (f x)

main :: IO ()
main = do
  print (twice (+ 3) 10)          -- 16
  print (twice (map (* 2)) [1, 2]) -- [4,8]

The parentheses in (a -> a) -> a -> a matter: the first argument is a function from a to a.

01 / 05