From f7a73ef8638e2863fd962a10b710fb3ea31414a5 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Mon, 20 May 2019 22:25:38 +0900 Subject: [PATCH 1/8] add some list functions --- lib-satysfi/dist/packages/list.satyg | 112 +++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/lib-satysfi/dist/packages/list.satyg b/lib-satysfi/dist/packages/list.satyg index 64ef6f562..4192ab0c4 100644 --- a/lib-satysfi/dist/packages/list.satyg +++ b/lib-satysfi/dist/packages/list.satyg @@ -18,6 +18,18 @@ module List val mapi-adjacent : (int -> 'a -> 'a option -> 'a option -> 'b) -> 'a list -> 'b list val length : 'a list -> int val nth : int -> 'a list -> 'a option + val head : 'a list -> 'a option + val tail : 'a list -> ('a list) option + val reverse-append : 'a list -> 'a list -> 'a list + val reverse-map : ('a -> 'b) -> 'a list -> 'b list + val for-all : ('a -> bool) -> 'a list -> bool + val exists : ('a -> bool) -> 'a list -> bool + val for-all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool option + val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool option + val find : ('a -> bool) -> 'a list -> 'a option + val partition : ('a -> bool) -> 'a list -> 'a list * 'a list + val split : ('a * 'b) list -> 'a list * 'b list + val combine : 'a list -> 'b list -> (('a * 'b) list) option end = struct @@ -132,4 +144,104 @@ module List in aux 0 lst + + let (^&&) b-opt1 b-opt2 = + match (b-opt1, b-opt2) with + | (Some(b1), Some(b2)) -> Some(b1 && b2) + | (_, _) -> None + + + let (^||) b-opt1 b-opt2 = + match (b-opt1, b-opt2) with + | (Some(b1), Some(b2)) -> Some(b1 || b2) + | (_, _) -> None + + + let (^::) a-opt1 lst-opt2 = + match (a-opt1, lst-opt2) with + | (Some(x), Some(xs)) -> Some(x :: xs) + | (_, _) -> None + + + + let head lst = + match lst with + | [] -> None + | head :: _ -> Some(head) + + + let tail lst = + match lst with + | [] -> None + | _ :: tail -> Some(tail) + + + let-rec reverse-append lst1 lst2 = + match lst1 with + | [] -> lst2 + | x :: xs -> reverse-append xs (x :: lst2) + + + let reverse-map f lst = + let-rec rmap-f accu lst = + match lst with + | [] -> [] + | (x :: xs) -> rmap-f (f x :: accu) xs + in + rmap-f [] lst + + + let-rec for-all f lst = + match lst with + | [] -> true + | x :: xs -> f x && for-all f xs + + + let-rec exists f lst = + match lst with + | [] -> false + | x :: xs -> f x || exists f xs + + + let-rec for-all2 f lst1 lst2 = + match (lst1, lst2) with + | ([], []) -> Some(true) + | (x1 :: xs1, x2 :: xs2) -> Some(f x1 x2) ^&& for-all2 f xs1 xs2 + | (_, _) -> None + + + let-rec exists2 f lst1 lst2 = + match (lst1, lst2) with + | ([], []) -> Some(false) + | (x1 :: xs1, x2 :: xs2) -> Some(f x1 x2) ^|| (exists2 f xs1 xs2) + | (_, _) -> None + + + let-rec find f lst = + match lst with + | [] -> None + | x :: xs -> if f x then Some(x) else find f xs + + + let partition f lst = + let-rec part yes no lst = + match lst with + | [] -> (reverse yes, reverse no) + | x :: xs -> if f x then part (x :: yes) no xs else part yes (x :: no) xs + in + part [] [] lst + + + let-rec split lst = + match lst with + | [] -> ([], []) + | (x, y) :: xs -> let (rx, ry) = split xs in (x :: rx, y :: ry) + + + let-rec combine lst1 lst2 = + match (lst1, lst2) with + | ([], []) -> Some([]) + | (x1 :: xs1, x2 :: xs2) -> Some((x1, x2)) ^:: (combine xs1 xs2) + | (_, _) -> None + end From 2e8defa3990177d8ce6f3237cb42587a1ea865c6 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Thu, 30 May 2019 17:59:06 +0900 Subject: [PATCH 2/8] fix --- lib-satysfi/dist/packages/list.satyg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib-satysfi/dist/packages/list.satyg b/lib-satysfi/dist/packages/list.satyg index 4192ab0c4..9f0ffed57 100644 --- a/lib-satysfi/dist/packages/list.satyg +++ b/lib-satysfi/dist/packages/list.satyg @@ -185,7 +185,7 @@ module List let reverse-map f lst = let-rec rmap-f accu lst = match lst with - | [] -> [] + | [] -> accu | (x :: xs) -> rmap-f (f x :: accu) xs in rmap-f [] lst From a3cee5564f854bd37e572f5b067e05ee2210f6da Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Fri, 12 Jul 2019 21:25:35 +0900 Subject: [PATCH 3/8] add bubblesort --- lib-satysfi/dist/packages/list.satyg | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib-satysfi/dist/packages/list.satyg b/lib-satysfi/dist/packages/list.satyg index 9f0ffed57..fc12fb5ac 100644 --- a/lib-satysfi/dist/packages/list.satyg +++ b/lib-satysfi/dist/packages/list.satyg @@ -30,6 +30,7 @@ module List val partition : ('a -> bool) -> 'a list -> 'a list * 'a list val split : ('a * 'b) list -> 'a list * 'b list val combine : 'a list -> 'b list -> (('a * 'b) list) option + val bubblesort : ('a -> 'a -> int) -> 'a list -> 'a list end = struct @@ -244,4 +245,33 @@ module List | (x1 :: xs1, x2 :: xs2) -> Some((x1, x2)) ^:: (combine xs1 xs2) | (_, _) -> None + + let-rec bubblesort f lst = + let-rec last lst = + match lst with + | x :: [] -> x + | _ :: xs -> last xs + in + let-rec init lst = + match lst with + | _ :: [] -> [] + | x :: xs -> x :: (init xs) + in + let-rec bs-sub lst = + match lst with + | (x :: y :: zs) -> ( + if (f x y) < 0 then + y :: bs-sub (x :: zs) + else + x :: bs-sub (y :: zs) + ) + | _ -> lst + in + let a = bs-sub lst in + let x = last a in + let xs = init a in + match xs with + | [] -> x :: [] + | _ -> x :: (bubblesort f xs) + end From 43f8d53b3a97aa2e7927d44b996a4aa22a70a78d Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Fri, 12 Jul 2019 21:41:33 +0900 Subject: [PATCH 4/8] add last and init --- lib-satysfi/dist/packages/list.satyg | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib-satysfi/dist/packages/list.satyg b/lib-satysfi/dist/packages/list.satyg index fc12fb5ac..4060a01b1 100644 --- a/lib-satysfi/dist/packages/list.satyg +++ b/lib-satysfi/dist/packages/list.satyg @@ -20,6 +20,8 @@ module List val nth : int -> 'a list -> 'a option val head : 'a list -> 'a option val tail : 'a list -> ('a list) option + val last : 'a list -> 'a option + val init : 'a list -> ('a list) option val reverse-append : 'a list -> 'a list -> 'a list val reverse-map : ('a -> 'b) -> 'a list -> 'b list val for-all : ('a -> bool) -> 'a list -> bool @@ -177,6 +179,20 @@ module List | _ :: tail -> Some(tail) + let-rec last lst = + match lst with + | [] -> None + | x :: [] -> Some(x) + | _ :: xs -> last xs + + + let-rec init lst = + match lst with + | [] -> None + | x :: [] -> Some([]) + | x :: xs -> Some(x) ^:: (init xs) + + let-rec reverse-append lst1 lst2 = match lst1 with | [] -> lst2 From f6d7e6248ff708c6d02bf46b0a1111accfadd751 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Fri, 12 Jul 2019 23:15:47 +0900 Subject: [PATCH 5/8] add take and drop --- lib-satysfi/dist/packages/list.satyg | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib-satysfi/dist/packages/list.satyg b/lib-satysfi/dist/packages/list.satyg index 4060a01b1..045f77b92 100644 --- a/lib-satysfi/dist/packages/list.satyg +++ b/lib-satysfi/dist/packages/list.satyg @@ -18,6 +18,8 @@ module List val mapi-adjacent : (int -> 'a -> 'a option -> 'a option -> 'b) -> 'a list -> 'b list val length : 'a list -> int val nth : int -> 'a list -> 'a option + val take : int -> 'a list -> 'a list + val drop : int -> 'a list -> 'a list val head : 'a list -> 'a option val tail : 'a list -> ('a list) option val last : 'a list -> 'a option @@ -167,6 +169,31 @@ module List + let-rec take i lst = + match lst with + | [] -> [] + | x :: xs -> ( + if i < 0 then + [] + else + x :: (take (i - 1) xs) + ) + + + let-rec drop i lst = + if i < 0 then + lst + else + match lst with + | [] -> [] + | x :: xs -> ( + if i < 1 then + xs + else + drop (i - 1) xs + ) + + let head lst = match lst with | [] -> None From 88580e11a9cc88e6722a5e3e6274327dee7951a5 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Fri, 12 Jul 2019 23:38:04 +0900 Subject: [PATCH 6/8] fix --- lib-satysfi/dist/packages/list.satyg | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib-satysfi/dist/packages/list.satyg b/lib-satysfi/dist/packages/list.satyg index 045f77b92..59491ffb5 100644 --- a/lib-satysfi/dist/packages/list.satyg +++ b/lib-satysfi/dist/packages/list.satyg @@ -171,7 +171,7 @@ module List let-rec take i lst = match lst with - | [] -> [] + | [] -> [] | x :: xs -> ( if i < 0 then [] @@ -185,7 +185,7 @@ module List lst else match lst with - | [] -> [] + | [] -> [] | x :: xs -> ( if i < 1 then xs @@ -208,14 +208,14 @@ module List let-rec last lst = match lst with - | [] -> None + | [] -> None | x :: [] -> Some(x) | _ :: xs -> last xs let-rec init lst = match lst with - | [] -> None + | [] -> None | x :: [] -> Some([]) | x :: xs -> Some(x) ^:: (init xs) @@ -310,11 +310,16 @@ module List ) | _ -> lst in - let a = bs-sub lst in - let x = last a in - let xs = init a in - match xs with - | [] -> x :: [] - | _ -> x :: (bubblesort f xs) + match lst with + | [] -> [] + | x :: [] -> [x] + | _ -> ( + let a = bs-sub lst in + let x = last a in + let xs = init a in + match xs with + | [] -> x :: [] + | _ -> x :: (bubblesort f xs) + ) end From cf4fbb0289ed951bc8bab2bc4f230ec368e08510 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Sat, 13 Jul 2019 18:55:20 +0900 Subject: [PATCH 7/8] add apply, null, all-and, or, show-opt, iterate, repeat, make-cycle, max and mini --- lib-satysfi/dist/packages/list.satyg | 86 ++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/lib-satysfi/dist/packages/list.satyg b/lib-satysfi/dist/packages/list.satyg index 59491ffb5..da32e06ef 100644 --- a/lib-satysfi/dist/packages/list.satyg +++ b/lib-satysfi/dist/packages/list.satyg @@ -18,6 +18,8 @@ module List val mapi-adjacent : (int -> 'a -> 'a option -> 'a option -> 'b) -> 'a list -> 'b list val length : 'a list -> int val nth : int -> 'a list -> 'a option + val apply : 'a -> ('a -> 'b) list -> 'b list + val null : 'a list -> bool val take : int -> 'a list -> 'a list val drop : int -> 'a list -> 'a list val head : 'a list -> 'a option @@ -26,6 +28,8 @@ module List val init : 'a list -> ('a list) option val reverse-append : 'a list -> 'a list -> 'a list val reverse-map : ('a -> 'b) -> 'a list -> 'b list + val all-and : bool list -> bool + val or : bool list -> bool val for-all : ('a -> bool) -> 'a list -> bool val exists : ('a -> bool) -> 'a list -> bool val for-all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool option @@ -34,6 +38,12 @@ module List val partition : ('a -> bool) -> 'a list -> 'a list * 'a list val split : ('a * 'b) list -> 'a list * 'b list val combine : 'a list -> 'b list -> (('a * 'b) list) option + val show-opt : ('a option) list -> 'a list + val iterate : int -> ('a -> 'a) -> 'a -> 'a list + val repeat : int -> 'a -> 'a list + val make-cycle : int -> 'a list -> 'a list + val max : ('a -> 'a -> int) -> 'a list -> 'a option + val mini : ('a -> 'a -> int) -> 'a list -> 'a option val bubblesort : ('a -> 'a -> int) -> 'a list -> 'a list end = struct @@ -168,6 +178,17 @@ module List | (_, _) -> None + let-rec apply v lst = + match lst with + | [] -> [] + | x :: xs -> x v :: apply v xs + + + let null lst = + match lst with + | [] -> true + | _ -> false + let-rec take i lst = match lst with @@ -234,6 +255,17 @@ module List in rmap-f [] lst + let-rec all-and lst = + match lst with + | [] -> true + | x :: xs -> x && all-and xs + + + let-rec or lst = + match lst with + | [] -> false + | x :: xs -> x || or xs + let-rec for-all f lst = match lst with @@ -289,6 +321,60 @@ module List | (_, _) -> None + let-rec show-opt lst = + match lst with + | [] -> [] + | x :: xs -> ( + match x with + | Some(x) -> x :: show-opt xs + | None -> show-opt xs + ) + + + let-rec iterate n f initial = + if n == 0 then + [] + else + f initial :: iterate (n - 1) f (f initial) + + let-rec repeat n initial = + if n == 0 then + [] + else + initial :: repeat (n - 1) initial + + + let-rec make-cycle n lst = + if n == 0 then + [] + else + append lst (make-cycle (n - 1) lst) + + + let-rec max f lst = + match lst with + | [] -> None + | x :: [] -> Some(x) + | x :: y :: zs -> ( + if (f x y) < 0 then + max f (y :: zs) + else + max f (x :: zs) + ) + + + let-rec mini f lst = + match lst with + | [] -> None + | x :: [] -> Some(x) + | x :: y :: zs -> ( + if (f x y) > 0 then + mini f (y :: zs) + else + mini f (x :: zs) + ) + + let-rec bubblesort f lst = let-rec last lst = match lst with From a53b40f5cd980146ae7b2b2ca7495346ae225abf Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Sat, 13 Jul 2019 21:14:34 +0900 Subject: [PATCH 8/8] takewhile, dropwhile, span, break --- lib-satysfi/dist/packages/list.satyg | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/lib-satysfi/dist/packages/list.satyg b/lib-satysfi/dist/packages/list.satyg index da32e06ef..92dcd8cf8 100644 --- a/lib-satysfi/dist/packages/list.satyg +++ b/lib-satysfi/dist/packages/list.satyg @@ -22,6 +22,11 @@ module List val null : 'a list -> bool val take : int -> 'a list -> 'a list val drop : int -> 'a list -> 'a list + val takewhile : ('a -> bool) -> 'a list -> 'a list + val dropwhile : ('a -> bool) -> 'a list -> 'a list + val splitat : int -> 'a list -> ('a list * 'a list) + val span : ('a -> bool) -> 'a list -> ('a list * 'a list) + val break : ('a -> bool) -> 'a list -> ('a list * 'a list) val head : 'a list -> 'a option val tail : 'a list -> ('a list) option val last : 'a list -> 'a option @@ -215,6 +220,60 @@ module List ) + let-rec takewhile f lst = + match lst with + | [] -> [] + | x :: xs -> ( + if f x then + x :: takewhile f xs + else + takewhile f xs + ) + + + let-rec dropwhile f lst = + match lst with + | [] -> [] + | x :: xs -> ( + if f x then + dropwhile f xs + else + x :: dropwhile f xs + ) + + + let splitat n lst = + (take n lst, drop n lst) + + + let span f lst = + let-rec span-sub lst1 lst2 = + match lst2 with + | y :: ys -> ( + if f y then + span-sub (y :: lst1) ys + else + (reverse lst1, lst2) + ) + | _ -> (reverse lst1, lst2) + in + span-sub [] lst + + + let break f lst = + let-rec break-sub lst1 lst2 = + match lst2 with + | y :: ys -> ( + if f y then + (reverse lst1, lst2) + else + break-sub (y :: lst1) ys + ) + | [] -> (reverse lst1, lst2) + in + break-sub [] lst + + let head lst = match lst with | [] -> None