fun greaterThan x =
fn y => x < y
fun filter (f, []) = []
| filter (f, x::xs) =
if f x
then x :: filter (f, xs)
else filter (f, xs)
fun countdown 0 = []
| countdown n = n :: countdown (n - 1)
fun shorterThan x =
fn y => String.size x > String.size y
fun shorterThan' x = let
val n = String.size x
in
fn y => n > String.size y
end
fun compose (f, g) =
fn x => f (g x)
val compose' =
fn f => fn g => fn x => f (g x)
fun compose'' f g x = f (g x)
fun sqrt_of_abs i = Math.sqrt (Real.fromInt (abs i))
fun sqrt_of_abs i = (Math.sqrt o Real.fromInt o abs) i
val sqrt_of_abs = Math.sqrt o Real.fromInt o abs
fun len [] = 0
| len (x::xs) = 1 + len xs
fun rev l = let
fun loop acc [] = acc
| loop acc (x::xs) = loop (x::acc) xs
in
loop [] l
end
fun map f [] = []
| map f (x::xs) = f x :: map f xs
fun filter f [] = []
| filter f (x::xs) =
if f x
then x :: filter f xs
else filter f xs
fun fold f acc [] = acc
| fold f acc (x::xs) = fold f (f acc x) xs
exception NotFound
fun nth [] _ = raise NotFound
| nth (x::xs) 0 = x
| nth (x::xs) i = nth xs (i - 1)
infix |>
fun x |> f = f x
fun strMap f s =
s |> String.explode
|> map f
|> String.implode
(*
fun strMapStupid f s =
String.implode (map f (String.explode s))
*)
fun strFilter f s =
s |> String.explode
|> filter f
|> String.implode
fun strLift (f: char list -> char list) s =
s |> String.explode
|> f
|> String.implode
fun strMap' f = strLift (map f)
fun strFilter' f = strLift (filter f)
fun split s = let
fun aux (w::ws) c =
if Char.isSpace c
then ([] :: w :: ws)
else ((c::w)::ws)
in
s |> String.explode
|> fold aux [[]]
|> map rev
|> rev
|> filter (fn l => l <> [])
|> map String.implode
end