練習問題 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]

効率は悪い.