Jump to content

Haskell/Solutions/More on functions

From Wikibooks, open books for an open world

← Back to More on functions

Exercises
  • Lambdas are a nice way to avoid defining unnecessary separate functions. Convert the following let- or where-bindings to lambdas:
    • map f xs where f x = x * 2 + 3
    • let f x y = read x + y in foldr f 1 xs
  • Sections are just syntactic sugar for lambda operations. I.e. (+2) is equivalent to \x -> x + 2. What would the following sections 'desugar' to? What would be their types?
    • (4+)
    • (1 `elem`)
    • (`notElem` "abc")
map (\x -> x * 2 + 3) xs
foldr (\x y -> read x + y) 1 xs

(\x -> 4 + x) :: Int -> Int
(\xs -> elem 1 xs) :: [Int] -> Bool
(\c -> notElem c "abc") :: Char -> Bool