Skip to content

Commit 43e3080

Browse files
committed
Add 'List.foldl1'
1 parent c5730f2 commit 43e3080

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

lib/List.trp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ let (* -- List Access -- *)
8383
fun foldl f y [] = y
8484
| foldl f y x::xs = foldl f (f (x,y)) xs
8585

86+
(** Left-fold of `f` on a non-empty list using the head as the initial value. *)
87+
fun foldl1 f x::xs = foldl f x xs
88+
8689
(* TODO: foldr *)
8790

8891
(** Returns the sublist of elements that satisfy `f`. Not tail-recursive. *)
@@ -183,6 +186,7 @@ let (* -- List Access -- *)
183186
map,
184187
mapi,
185188
foldl,
189+
foldl1,
186190
filter,
187191
filteri,
188192
partition,

tests/lib/List.golden

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2025-09-09T14:12:55.766Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
1+
2025-10-24T08:31:14.889Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
22
begin List
33
begin head
44
[ TEST ] it returns 42 for [42] [ PASS ] it returns 42 for [42]
@@ -91,6 +91,21 @@
9191
[ TEST ] it id [1,1] [ PASS ] it id [1,1]
9292
[ TEST ] it id [1,3] [ PASS ] it id [1,3]
9393
end 
94+
begin foldl
95+
[ TEST ] it sums 0 [] [ PASS ] it sums 0 []
96+
[ TEST ] it sums 2 [] [ PASS ] it sums 2 []
97+
[ TEST ] it sums 0 [1] [ PASS ] it sums 0 [1]
98+
[ TEST ] it sums 0 [1,1] [ PASS ] it sums 0 [1,1]
99+
[ TEST ] it sums 0 [1,3] [ PASS ] it sums 0 [1,3]
100+
[ TEST ] it sums 2 [1,3] [ PASS ] it sums 2 [1,3]
101+
end 
102+
begin foldl1
103+
[ TEST ] it sums [0] [ PASS ] it sums [0]
104+
[ TEST ] it sums [1] [ PASS ] it sums [1]
105+
[ TEST ] it sums [2] [ PASS ] it sums [2]
106+
[ TEST ] it sums [1,1] [ PASS ] it sums [1,1]
107+
[ TEST ] it sums [1,3] [ PASS ] it sums [1,3]
108+
end 
94109
begin filter
95110
[ TEST ] it isOdd [] [ PASS ] it isOdd []
96111
[ TEST ] it isEven [] [ PASS ] it isEven []
@@ -128,6 +143,6 @@
128143
end 
129144
end 
130145

131-
Total: 90
132-
Passes: 90
146+
Total: 101
147+
Passes: 101
133148
>>> Main thread finished with value: true@{}%{}

tests/lib/List.trp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ let val tests = Unit.group "List" [
105105
, Unit.it "id [1,3]" (Unit.isEq [(0,1), (1,3)] (List.mapi id [1,3]))
106106
]
107107
end,
108+
let fun sum (a,b) = a+b
109+
in Unit.group "foldl" [
110+
Unit.it "sums 0 []" (Unit.isEq 0 (List.foldl sum 0 []))
111+
, Unit.it "sums 2 []" (Unit.isEq 2 (List.foldl sum 2 []))
112+
, Unit.it "sums 0 [1]" (Unit.isEq 1 (List.foldl sum 0 [1]))
113+
, Unit.it "sums 0 [1,1]" (Unit.isEq 2 (List.foldl sum 0 [1,1]))
114+
, Unit.it "sums 0 [1,3]" (Unit.isEq 4 (List.foldl sum 0 [1,3]))
115+
, Unit.it "sums 2 [1,3]" (Unit.isEq 6 (List.foldl sum 2 [1,3]))
116+
]
117+
end,
118+
let fun sum (a,b) = a+b
119+
in Unit.group "foldl1" [
120+
Unit.it "sums [0]" (Unit.isEq 0 (List.foldl1 sum [0]))
121+
, Unit.it "sums [1]" (Unit.isEq 1 (List.foldl1 sum [1]))
122+
, Unit.it "sums [2]" (Unit.isEq 2 (List.foldl1 sum [2]))
123+
, Unit.it "sums [1,1]" (Unit.isEq 2 (List.foldl1 sum [1,1]))
124+
, Unit.it "sums [1,3]" (Unit.isEq 4 (List.foldl1 sum [1,3]))
125+
]
126+
end,
108127
let val xs = [0,1,2,3,4,5,6,7,8,9]
109128
fun isOdd x = (x mod 2) = 1
110129
fun isEven x = (x mod 2) = 0

0 commit comments

Comments
 (0)