411 REFERENCE · Wikibooks
Haskell/Solutions/More on functions
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Operators
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")1.
Substitute f to get map (\ x -> x * 2 + 3) xs
Substitute f to get foldr (\ x y -> read x + y) 1 xs
2.
(4+)
Becomes (\ x -> 4 + x)
Has type Num a => a -> a
(1 `elem`)
Becomes(\ x -> 1 `elem` x)
Alternately written as (\ x -> elem 1 x)
Has type Num a :: a -> Bool
Note: The full type is (Foldable t, Eq a, Num a) => t a -> Bool but this has not been covered yet.
(`notElem` "abc")
Becomes (\ x -> x `notElem` "abc")
Alternately written as (\ x -> notElem x "abc")
Has type Char -> Bool