練習問題 4.5.2
練習問題4.3.7で議論したtakeWhileと
dropWhileという2つの関数を考える.
takeWhileを
foldrの具体例として定義せよ.
dropWhileは畳み込み関数の具体例として定義できるか.
takeWhile :: (a -> Bool) -> [a] -> [a]
takeWhile p = foldr f []
where
f x xs = if p x then x:xs else []
dropWhile :: (a -> Bool) -> [a] -> [a]
dropWhile p = foldl f []
where
f xs y | null xs && p y = []
| otherwise = xs ++ [y]
効率は悪い.