От: | dr.Chaos | Украшения HandMade | |
Дата: | 21.03.07 11:16 | ||
Оценка: |
filter p [] = [] filter p (x:xs) = if p x then x : filter p xs else filter p xs
...
Now, suppose that we call the result of calling filter p xs simply b, then we can rewrite this as:
filter p [] = [] filter p (x:xs) = if p x then x : b else b filter p = foldr (\a b -> if p a then a:b else b) []
(++) [] ys = ys (++) (x:xs) ys = x : (xs ++ ys)
Now, the question is whether we can write this in fold notation. First, we can apply eta reduction to the first line to give:
(++) [] = id
Through a sequence of steps, we can also eta-reduce the second line:
(++) (x:xs) ys = x : ((++) xs ys) ==> (++) (x:xs) ys = (x:) ((++) xs ys) ==> (++) (x:xs) ys = ((x:) . (++) xs) ys --Здесь может имелось в виду ((x:) . (++)) xs ys? ==> (++) (x:xs) = (x:) . (++) xs