isInAny needle haystack = any inSequence haystack
where inSequence s = needle `isInfixOf` s
isInAny2 needle haystack = any (\s -> needle `isInfixOf` s) haystack
inc = \x -> x + 1
plus = \a b -> a + b
three = (\x -> x + 1) 2
let
oder where
ghci> :type dropWhile
dropWhile :: (a -> Bool) -> [a] -> [a]
ghci> :type dropWhile isSpace
dropWhile isSpace :: [Char] -> [Char]
ghci> map (dropWhile isSpace) [" a","f"," e"]
["a","f","e"]
Currying /= Partial function application (?)
Point-free style oder Tacit programming
niceSum :: [Integer] -> Integer
niceSum xs = foldl (+) 0 xs
niceSum2 :: [Integer] -> Integer
niceSum2 = foldl (+) 0
ghci> (1+) 2
3
ghci> map (*3) [24,36]
[72,108]
ghci> map (2^) [3,5,7,9]
[8,32,128,512]
ghci> :type elem
elem :: Eq a => a -> [a] -> Bool
ghci> :type (`elem` ['a'..'z'])
(`elem` ['a'..'z']) :: Char -> Bool
@
zwischen Name und Patternsuffixes :: [a] -> <a class="internal" href="/wiki/lernfp/a">a</a>
suffixes xs@(_:xs') = xs : suffixes xs'
suffixes _ = []
suffixes2 xs = init (tails xs)
compose f g x = f (g x)
suffixes3 xs = compose init tails xs
suffixes4 = compose init tails
(.) :: (b -> c) -> (a -> b) -> a -> c
suffixes5 = init . tails
capCount = length . filter (isUpper . head) . words
capCount "Hello there, Mom!" == 2
(.)
ist rechtsassoziativlet
oder where
fold
für welche Anwendungsfälle?foldr
für Erzeugen von Listenfoldl'
ansonstenfoldl
meidenseq :: a -> t -> t
foldl' _ zero [] = zero
foldl' step zero (x:xs) =
let new = step zero x
in new `seq` foldl' step new xs
seq
muss innerhalb eines Ausdrucks zuerst evaluiert werdenseq
hat keinen Effekt auf duplizierte AusdrückehiddenInside x y = someFunc (x `seq` y)
hiddenByLet x y z = let a = x `seq` someFunc y
in anotherFunc a z
badExpression step zero (x:xs) =
seq (step zero x)
(badExpression step (step zero x) xs)
seq
verketten um mehrere Ausdrücke zu evaluieren:chained x y z = x `seq` y `seq` someFunc z
seq
stoppt bei KonstruktorenstrictPair (a,b) = a `seq` b `seq` (a,b)
strictList (x:xs) = x `seq` x : strictList xs
strictList [] = []
seq
ist kein Allheilmittel