Skip to content

Instantly share code, notes, and snippets.

@jhrr
Last active December 27, 2015 18:19
Show Gist options
  • Save jhrr/7368977 to your computer and use it in GitHub Desktop.
Save jhrr/7368977 to your computer and use it in GitHub Desktop.
(* 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