From 8638e428e2936b9a187fd88787e5d8a44f4bf62c Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Oct 2024 13:54:40 -0700 Subject: [PATCH] Add comparator tuple fold --- src/exam01/exam01.sml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/exam01/exam01.sml b/src/exam01/exam01.sml index 1ad8bea..f53948a 100644 --- a/src/exam01/exam01.sml +++ b/src/exam01/exam01.sml @@ -23,4 +23,12 @@ fun insert comp add li = val testInsert = insert (fn (a, b) => a < b) 5 [1, 2, 3, 4, 6, 7, 8]; (* should be [1, 2, 3, 4, 5, 6, 7, 8] *) raiseIfFalse(testInsert = [1, 2, 3, 4, 5, 6, 7, 8]); -(* remove question *) \ No newline at end of file +(* map to tuple question *) +fun foo f li = map (fn (cur) => (cur, f(cur))) li; +val bar = foo (fn x => x * x) [1, 2, 3, 4]; (* should be [(1, 1), (2, 4), (3, 9), (4, 16)] *) +raiseIfFalse(bar = [(1, 1), (2, 4), (3, 9), (4, 16)]); + +(* remove symmetric dups question *) +fun foo f li = foldr (fn ((cur1, cur2), acc) => if f(cur1) = f(cur2) then (cur1, cur2)::acc else acc) [] li; + +