programming_languages/sml/week4/list.sml

60 lines
1.2 KiB
Standard ML
Raw Permalink Normal View History

2020-06-05 04:57:25 +03:00
use "operators.sml";
fun cons head tail = head :: tail
2020-06-16 00:58:09 +03:00
fun append a b = a @ b
2020-06-05 04:57:25 +03:00
fun foldl f acc lst =
case lst of
[] => acc
| head :: tail => foldl f (f head acc) tail
fun reverse lst = foldl cons [] lst
2020-07-01 23:48:56 +03:00
fun sort' f lst =
case lst of
[] => []
| [one] => [one]
| first :: second :: rest =>
if f first second
then first :: sort' f (second :: rest)
else second :: sort' f (first :: rest)
(* only for sorted lists *)
fun distinct lst =
case lst of
[] => true
| [_] => true
| first :: second :: rest =>
if first = second
then false
else distinct (second :: rest)
(* simple recursive convergence *)
fun fix f g x =
if f g x = x
then x
else fix f g (f g x)
(* naive bubble sort *)
fun sort f x = fix sort' f x
fun foldr f acc lst = lst |> reverse |> foldl f acc
2020-06-05 04:57:25 +03:00
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