programming_languages/sml/week4/list.sml
2020-06-16 00:58:09 +03:00

33 lines
No EOL
624 B
Standard ML

use "operators.sml";
fun cons head tail = head :: tail
fun append a b = a @ b
fun foldl f acc lst =
case lst of
[] => acc
| head :: tail => foldl f (f head acc) tail
fun reverse lst = foldl cons [] lst
(* wrong implementation *)
fun foldr f acc = foldl f acc >> reverse
fun map f = foldr (f >> cons) []
fun filter predicate lst =
let
fun f elm acc = if predicate elm then elm :: acc else acc
in
foldr f [] lst
end
fun empty lst = lst = []
(* not efficient but works *)
fun exists elem lst =
lst
|> filter (fn needle => elem = needle)
|> empty
|> not