Updated some functions, renamed, added from_to

This commit is contained in:
radrow 2019-08-29 13:41:04 +02:00
parent 60528e9128
commit 1d962f2001
2 changed files with 32 additions and 18 deletions

View File

@ -37,12 +37,14 @@ namespace List =
[] => reverse(acc) [] => reverse(acc)
h::t => find_indices_(p, t, n+1, if(p(h)) n::acc else acc) h::t => find_indices_(p, t, n+1, if(p(h)) n::acc else acc)
function nth(n : int, l : list('a)) : option('a) = switch(l) function nth(n : int, l : list('a)) : option('a) =
if(n < 0) None else switch(l)
[] => None [] => None
h::t => if(n == 0) Some(h) else nth(n-1, t) h::t => if(n == 0) Some(h) else nth(n-1, t)
/* Unsafe version of `nth` */ /* Unsafe version of `nth` */
function get(n : int, l : list('a)) : 'a = switch(l) function get(n : int, l : list('a)) : 'a =
if(n < 0) abort("Negative index get") else switch(l)
[] => abort("Out of index get") [] => abort("Out of index get")
h::t => if(n == 0) h else get(n-1, t) h::t => if(n == 0) h else get(n-1, t)
@ -53,6 +55,15 @@ namespace List =
_::t => length_(t, acc + 1) _::t => length_(t, acc + 1)
function from_to(a : int, b : int) : list(int) = from_to_(a, b, [])
private function from_to_(a, b, acc) =
if (a > b) acc else from_to_(a, b - 1, b :: acc)
function from_to_step(a : int, b : int, s : int) : list(int) = from_to_step_(a, b, s, [])
private function from_to_step_(a, b, s, acc) =
if (a > b) reverse(acc) else from_to_step_(a + s, b, s, a :: acc)
/* Unsafe. Replaces `n`th element of `l` with `e`. Crashes on over/underflow */ /* Unsafe. Replaces `n`th element of `l` with `e`. Crashes on over/underflow */
function replace_at(n : int, e : 'a, l : list('a)) : list('a) = function replace_at(n : int, e : 'a, l : list('a)) : list('a) =
if(n<0) abort("insert_at underflow") else replace_at_(n, e, l, []) if(n<0) abort("insert_at underflow") else replace_at_(n, e, l, [])
@ -71,14 +82,17 @@ namespace List =
[] => abort("insert_at overflow") [] => abort("insert_at overflow")
h::t => insert_at_(n-1, e, t, h::acc) h::t => insert_at_(n-1, e, t, h::acc)
function insert_by(f : (('a, 'a) => bool), x : 'a, l : list('a)) : list('a) = function insert_by(cmp : (('a, 'a) => bool), x : 'a, l : list('a)) : list('a) =
insert_by_(cmp, x, l, [])
private function insert_by_(cmp : (('a, 'a) => bool), x : 'a, l : list('a), acc : list('a)) : list('a) =
switch(l) switch(l)
[] => [x] [] => reverse(x::acc)
(e :: l') => h::t =>
if(f(x, e)) if(cmp(x, e)) // x < e
e :: insert_by(f, x, l') reverse(acc) ++ (x::l)
else else
x :: l insert_by(cmp, x, t, e::acc)
function foldr(cons : ('a, 'b) => 'b, nil : 'b, l : list('a)) : 'b = switch(l) function foldr(cons : ('a, 'b) => 'b, nil : 'b, l : list('a)) : 'b = switch(l)
[] => nil [] => nil
@ -88,12 +102,12 @@ namespace List =
[] => acc [] => acc
h::t => foldl(rcons, rcons(acc, h), t) h::t => foldl(rcons, rcons(acc, h), t)
function foreach(f : 'a => unit, l : list('a)) : unit = function foreach(l : list('a), f : 'a => unit) : unit =
switch(l) switch(l)
[] => () [] => ()
e::l' => e::l' =>
f(e) f(e)
foreach(f, l') foreach(l', f)
function reverse(l : list('a)) : list('a) = foldl((lst, el) => el :: lst, [], l) function reverse(l : list('a)) : list('a) = foldl((lst, el) => el :: lst, [], l)
@ -149,7 +163,7 @@ namespace List =
h::t => if(p(h)) partition_(p, t, h::acc_t, acc_f) else partition_(p, t, acc_t, h::acc_f) h::t => if(p(h)) partition_(p, t, h::acc_t, acc_f) else partition_(p, t, acc_t, h::acc_f)
function concats(ll : list(list('a))) : list('a) = foldr((l1, l2) => l1 ++ l2, [], ll) function flatten(ll : list(list('a))) : list('a) = foldr((l1, l2) => l1 ++ l2, [], ll)
function all(p : 'a => bool, l : list('a)) : bool = switch(l) function all(p : 'a => bool, l : list('a)) : bool = switch(l)
[] => true [] => true

View File

@ -19,7 +19,7 @@ namespace Option =
function force(o : option('a)) : 'a = default(abort("Forced None value"), o) function force(o : option('a)) : 'a = default(abort("Forced None value"), o)
function on_elem(f : 'a => unit, o : option('a)) : unit = match((), f, o) function on_elem(o : option('a), f : 'a => unit) : unit = match((), f, o)
function map(f : 'a => 'b, o : option('a)) : option('b) = switch(o) function map(f : 'a => 'b, o : option('a)) : option('b) = switch(o)
None => None None => None