From 1d962f200153fa4b40003a875f52163f3293f01d Mon Sep 17 00:00:00 2001 From: radrow Date: Thu, 29 Aug 2019 13:41:04 +0200 Subject: [PATCH 1/5] Updated some functions, renamed, added from_to --- priv/stdlib/List.aes | 48 +++++++++++++++++++++++++++--------------- priv/stdlib/Option.aes | 2 +- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/priv/stdlib/List.aes b/priv/stdlib/List.aes index cdd2464..637faeb 100644 --- a/priv/stdlib/List.aes +++ b/priv/stdlib/List.aes @@ -37,14 +37,16 @@ namespace List = [] => reverse(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) - [] => None - h::t => if(n == 0) Some(h) else nth(n-1, t) + function nth(n : int, l : list('a)) : option('a) = + if(n < 0) None else switch(l) + [] => None + h::t => if(n == 0) Some(h) else nth(n-1, t) /* Unsafe version of `nth` */ - function get(n : int, l : list('a)) : 'a = switch(l) - [] => abort("Out of index get") - h::t => if(n == 0) h else get(n-1, t) + function get(n : int, l : list('a)) : 'a = + if(n < 0) abort("Negative index get") else switch(l) + [] => abort("Out of index get") + h::t => if(n == 0) h else get(n-1, t) function length(l : list('a)) : int = length_(l, 0) @@ -53,6 +55,15 @@ namespace List = _::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 */ 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, []) @@ -71,14 +82,17 @@ namespace List = [] => abort("insert_at overflow") 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) - [] => [x] - (e :: l') => - if(f(x, e)) - e :: insert_by(f, x, l') + [] => reverse(x::acc) + h::t => + if(cmp(x, e)) // x < e + reverse(acc) ++ (x::l) else - x :: l + insert_by(cmp, x, t, e::acc) + function foldr(cons : ('a, 'b) => 'b, nil : 'b, l : list('a)) : 'b = switch(l) [] => nil @@ -88,12 +102,12 @@ namespace List = [] => acc 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) - [] => () - e :: l' => + [] => () + e::l' => f(e) - foreach(f, l') + foreach(l', f) 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) - 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) [] => true diff --git a/priv/stdlib/Option.aes b/priv/stdlib/Option.aes index 843e62d..6ebf98c 100644 --- a/priv/stdlib/Option.aes +++ b/priv/stdlib/Option.aes @@ -19,7 +19,7 @@ namespace Option = 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) None => None From 076d635dbeb01a92939ddb46ad3de862058802bb Mon Sep 17 00:00:00 2001 From: radrow Date: Thu, 29 Aug 2019 15:32:10 +0200 Subject: [PATCH 2/5] Fix errors --- priv/stdlib/List.aes | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/priv/stdlib/List.aes b/priv/stdlib/List.aes index 637faeb..8b070b5 100644 --- a/priv/stdlib/List.aes +++ b/priv/stdlib/List.aes @@ -38,13 +38,15 @@ namespace List = h::t => find_indices_(p, t, n+1, if(p(h)) n::acc else acc) function nth(n : int, l : list('a)) : option('a) = - if(n < 0) None else switch(l) + if(n < 0) None + else switch(l) [] => None h::t => if(n == 0) Some(h) else nth(n-1, t) /* Unsafe version of `nth` */ function get(n : int, l : list('a)) : 'a = - if(n < 0) abort("Negative index get") else switch(l) + if(n < 0) abort("Negative index get") + else switch(l) [] => abort("Out of index get") h::t => if(n == 0) h else get(n-1, t) @@ -88,10 +90,10 @@ namespace List = switch(l) [] => reverse(x::acc) h::t => - if(cmp(x, e)) // x < e + if(cmp(x, h)) // x < h reverse(acc) ++ (x::l) else - insert_by(cmp, x, t, e::acc) + insert_by_(cmp, x, t, h::acc) function foldr(cons : ('a, 'b) => 'b, nil : 'b, l : list('a)) : 'b = switch(l) From 256aadd5754708f8d13979d9f12f28ce0aa2fcb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Rowicki?= <35342116+radrow@users.noreply.github.com> Date: Fri, 30 Aug 2019 13:44:26 +0200 Subject: [PATCH 3/5] [......] Co-Authored-By: Ulf Norell --- priv/stdlib/List.aes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/priv/stdlib/List.aes b/priv/stdlib/List.aes index 8b070b5..2113bc4 100644 --- a/priv/stdlib/List.aes +++ b/priv/stdlib/List.aes @@ -57,7 +57,7 @@ namespace List = _::t => length_(t, acc + 1) - function from_to(a : int, b : int) : list(int) = from_to_(a, b, []) + function from_to(a : int, b : int) : list(int) = [a..b] private function from_to_(a, b, acc) = if (a > b) acc else from_to_(a, b - 1, b :: acc) From 71a556ce81726908aad08e0d2428486a5b9b61af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Rowicki?= <35342116+radrow@users.noreply.github.com> Date: Fri, 30 Aug 2019 13:46:02 +0200 Subject: [PATCH 4/5] nth update --- priv/stdlib/List.aes | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/priv/stdlib/List.aes b/priv/stdlib/List.aes index 2113bc4..57d2250 100644 --- a/priv/stdlib/List.aes +++ b/priv/stdlib/List.aes @@ -38,16 +38,14 @@ namespace List = h::t => find_indices_(p, t, n+1, if(p(h)) n::acc else acc) function nth(n : int, l : list('a)) : option('a) = - if(n < 0) None - else switch(l) + switch(l) [] => None h::t => if(n == 0) Some(h) else nth(n-1, t) /* Unsafe version of `nth` */ function get(n : int, l : list('a)) : 'a = - if(n < 0) abort("Negative index get") - else switch(l) - [] => abort("Out of index get") + switch(l) + [] => abort(if(n < 0) "Negative index get" else "Out of index get") h::t => if(n == 0) h else get(n-1, t) From 6408969cd31dac7631e383c4a799034d1d23566a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Rowicki?= <35342116+radrow@users.noreply.github.com> Date: Fri, 30 Aug 2019 14:06:46 +0200 Subject: [PATCH 5/5] Remove from_to_ --- priv/stdlib/List.aes | 2 -- 1 file changed, 2 deletions(-) diff --git a/priv/stdlib/List.aes b/priv/stdlib/List.aes index 57d2250..97cf126 100644 --- a/priv/stdlib/List.aes +++ b/priv/stdlib/List.aes @@ -56,8 +56,6 @@ namespace List = function from_to(a : int, b : int) : list(int) = [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) =