Skip to content

Instantly share code, notes, and snippets.

@gofer
Last active December 30, 2015 14:19
Show Gist options
  • Save gofer/7841607 to your computer and use it in GitHub Desktop.
Save gofer/7841607 to your computer and use it in GitHub Desktop.
Map, Reduce, Filter
(* 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