Dmitry Boulytchev 2020-09-23 01:39:38 +03:00
parent 4c736914d4
commit 2f640fdc68
9 changed files with 29 additions and 3 deletions

Binary file not shown.

View file

@ -347,7 +347,9 @@ fresh list if images in the same order.}
"\lstinline|Some (v)|" otherwise, where "\lstinline|v|"~--- the first value to satisfy "\lstinline|f|".
}
\descr{\lstinline|fun flatten (l)|}{Flattens an arbitrary nesting of lists into a regular list. The order of elements is preserved in both senses.}
\descr{\lstinline|fun flatten (l)|}{Flattens a list of lists into a regular list. The order of elements is preserved in both senses.}
\descr{\lstinline|fun deepFlatten (l)|}{Flattens an arbitrary nesting of lists of \emph{boxed} values into a regular list. The order of elements is preserved in both senses.}
\descr{\lstinline|fun zip (a, b)|}{Zips a pair of lists into the list of pairs. Does not work for lists of different lengths.}

View file

@ -1 +1 @@
let path = "/home/db/.opam/4.08.1/share/Lama"
let path = "/home/db/.opam/ocaml-system.4.07.1/share/Lama"

View file

@ -1 +1 @@
let version = "Version 1.00, d06d9a250, Mon Sep 21 21:30:08 2020 +0300"
let version = "Version 1.00, 4c736914d, Mon Sep 21 23:46:13 2020 +0300"

View file

@ -84,6 +84,21 @@ public fun flatten (l) {
curr [0] := new
}
iter (fun (x) {iter (append, x)}, l);
res [1]
}
public fun deepFlatten (l) {
local res = [0, {}], curr = [res];
fun append (x) {
local new = x : {};
curr [0][1] := new;
curr [0] := new
}
fun traverse (l) {
case l of
_ : _ -> iter (traverse, l)

View file

@ -1,4 +1,5 @@
Flattening: 0
Flattening: {0, 0, 0, 0}
Flattening: 0
Flattening: {1, 2, 3}
Flattening: {1, 2, 3, 4, 5, 6, 7, 8, 9}

View file

@ -0,0 +1,2 @@
Flattening: 0
Flattening: {A, B, C, D}

View file

@ -2,6 +2,7 @@ import List;
import Array;
printf ("Flattening: %s\n", flatten ({}).string);
printf ("Flattening: %s\n", flatten ({{0}, {0, 0, 0}}).string);
printf ("Flattening: %s\n", flatten ({{}, {}, {}}).string);
printf ("Flattening: %s\n", flatten ({1, 2, 3} : {}).string);
printf ("Flattening: %s\n", flatten ({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}).string);

View file

@ -0,0 +1,5 @@
import List;
import Array;
printf ("Flattening: %s\n", deepFlatten ({}).string);
printf ("Flattening: %s\n", deepFlatten ({{A}, {B, {C}, D}}).string)