Last active
December 27, 2015 18:19
-
-
Save jhrr/7368977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* map *) | |
fun map(_, nil) = nil | |
| map(f, (head::tail)) = f(head) :: map(f, tail) | |
(* filter *) | |
fun filter(_, nil) = nil | |
| filter(p, (head::tail)) = if p(head) | |
then (head :: filter(p, tail)) | |
else filter(p, tail) | |
(* fold *) | |
fun fold(_, v, nil) = v | |
| fold(f, v, (head::tail)) = f(head, fold(f, v, tail)) | |
(* map and filter defined in terms of fold *) | |
fun map(f, L) = fold((fn x, xs => (f(x):xs)), [], L) | |
fun filter(p, L) = fold((fn x, xs => if p(x) then x:xs else xs end), [], L) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment