Declaration vs. expression style
1.1 Declaration style
filter1 :: (a -> Bool) -> [a] -> [a] filter1 p [] = [] filter1 p (x:xs) | p x = x : rest | otherwise = rest where rest = filter p xs
1.2 Expression style
filter2 :: (a -> Bool) -> [a] -> [a] filter2 = \p -> \ xs -> case xs of [] -> [] (x:xs) -> let rest = filter2 p xs in if p x then x : rest else rest
ghci> filter2 (even) [1,2,3,4] [2,4] ghci> filter1 (odd) [1,2,3,4] [1,3] ghci> filter2 (\x-> x > 2) [1,2,3,4] [3,4]
"filter2 = \p -> \ xs ->" という書き方が出来るということは。
ghci> foldl (\x y -> x + y ) 0 [1,2,3] 6 ghci> foldl (\x -> \y -> x + y ) 0 [1,2,3] 6
(\x y -> x + y ) と (\x -> \y -> x + y ) は同等。
ghci> let f = \a -> \b -> \c -> a + b + c ghci> f 1 2 3 6 ghci> let f = \a -> \b -> \c -> a + b + c ghci> f 1 2 3 6 ghci> let f = \a b -> \c -> a + b + c ghci> f 1 2 3 6 ghci> let f = \a b c -> a + b + c ghci> f 1 2 3 6 ghci> let f = \a -> \b c -> a + b + c ghci> f 1 2 3 6 ghci> let f a b c = a + b + c ghci> f 1 2 3 6 ghci> let f a = \b c -> a + b + c ghci> f 1 2 3 6