Last active
December 30, 2015 14:19
-
-
Save gofer/7841607 to your computer and use it in GitHub Desktop.
Map, Reduce, Filter
This file contains hidden or 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 f nil = nil | |
| map f (x::xs) = (f x)::(map f xs); | |
(* reduce *) | |
fun reduce f x0 nil = x0 | |
| reduce f x0 (x::nil) = f x x0 | |
| reduce f x0 (x::xs) = f x (reduce f x0 xs); | |
(* filter *) | |
fun filter p nil = nil | |
| filter p (x::xs) = let | |
val ys = filter p xs | |
in | |
if (p x) then x::ys else ys | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment