From 8f04414e9bf735737b4a78865134ad413b7365b1 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 16 Oct 2024 13:44:46 -0700 Subject: [PATCH] Curry functions --- src/exam01/exam01.sml | 26 ++++++++++++++++++++++++++ src/quiz04/answers.sml | 20 ++++++++++++++++++++ src/quiz04/quiz04.sml | 27 +++++++++++++-------------- src/quiz04/quiz07.sml | 19 ------------------- 4 files changed, 59 insertions(+), 33 deletions(-) create mode 100644 src/exam01/exam01.sml create mode 100644 src/quiz04/answers.sml delete mode 100644 src/quiz04/quiz07.sml diff --git a/src/exam01/exam01.sml b/src/exam01/exam01.sml new file mode 100644 index 0000000..1ad8bea --- /dev/null +++ b/src/exam01/exam01.sml @@ -0,0 +1,26 @@ + +fun raiseIfFalse(result) = if result then print("\nTest Passed\n") else raise Fail "Test failed"; + +(* char lengths question *) +fun charLengths li = foldl (fn (cur, acc) => acc + length(explode(cur))) 0 li; + +val testCharLengths = charLengths ["hello", "world", "this", "is", "a", "test"]; (* should be 21 *) +raiseIfFalse(testCharLengths = 21); + + +(* insert question *) +fun insert comp add li = + let + fun getLeft comp add li = foldr (fn (cur, acc) => if comp(cur, add) then cur::acc else acc) [] li; + fun getRight comp add li = foldr (fn (cur, acc) => if comp(add, cur) then cur::acc else acc) [] li; + val left = getLeft comp add li; + val right = getRight comp add li; + in + left @ [add] @ right + end + + +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 diff --git a/src/quiz04/answers.sml b/src/quiz04/answers.sml new file mode 100644 index 0000000..b29f48d --- /dev/null +++ b/src/quiz04/answers.sml @@ -0,0 +1,20 @@ +fun foo l = map real l; +foo [1,2,3]; + +fun foo l = foldr (fn (e, li) => e::e::li) [] l; +foo [1,2,3]; + +fun foo p l = foldr (fn (e, (lt, gt)) => if e <= p then (e::lt,gt) else (lt,e::gt)) ([],[]) l; +foo 3 [1,2,3,4,5]; + +fun cubelist li = map (fn(cur) => cur*cur*cur) li; +cubelist [1,2,3]; + +fun removeAll li x = foldr (fn (cur, acc) => if cur = x then acc else cur::acc) [] li; +removeAll [1,2,3,4,5,6,7,8,9,10] 5; + +(* You can rearrange the order of the params like this *) +fun removeAll x li = foldr (fn (cur, acc) => if cur = x then acc else cur::acc) [] li; + +(* But then the call site must be updated to reflect the new order *) +removeAll 0 [1,2,0,3,4,0,5]; \ No newline at end of file diff --git a/src/quiz04/quiz04.sml b/src/quiz04/quiz04.sml index b29f48d..aba4e82 100644 --- a/src/quiz04/quiz04.sml +++ b/src/quiz04/quiz04.sml @@ -1,20 +1,19 @@ -fun foo l = map real l; -foo [1,2,3]; -fun foo l = foldr (fn (e, li) => e::e::li) [] l; -foo [1,2,3]; -fun foo p l = foldr (fn (e, (lt, gt)) => if e <= p then (e::lt,gt) else (lt,e::gt)) ([],[]) l; -foo 3 [1,2,3,4,5]; +(* cubeLost *) -fun cubelist li = map (fn(cur) => cur*cur*cur) li; -cubelist [1,2,3]; +fun cubeList li = map (fn(cur) => cur * cur * cur) li; -fun removeAll li x = foldr (fn (cur, acc) => if cur = x then acc else cur::acc) [] li; -removeAll [1,2,3,4,5,6,7,8,9,10] 5; +cubeList([1,2,3]); -(* You can rearrange the order of the params like this *) -fun removeAll x li = foldr (fn (cur, acc) => if cur = x then acc else cur::acc) [] li; +(* removeAll *) -(* But then the call site must be updated to reflect the new order *) -removeAll 0 [1,2,0,3,4,0,5]; \ No newline at end of file +fun removeAll li x = foldr (fn(cur, acc) => if cur = x then acc else cur::acc) [] li; + +val x = removeAll [1,2,3,3,4] 4; + +(* map question *) + +fun r li = map real li + +val res = r [1,2,3] \ No newline at end of file diff --git a/src/quiz04/quiz07.sml b/src/quiz04/quiz07.sml deleted file mode 100644 index aba4e82..0000000 --- a/src/quiz04/quiz07.sml +++ /dev/null @@ -1,19 +0,0 @@ - - -(* cubeLost *) - -fun cubeList li = map (fn(cur) => cur * cur * cur) li; - -cubeList([1,2,3]); - -(* removeAll *) - -fun removeAll li x = foldr (fn(cur, acc) => if cur = x then acc else cur::acc) [] li; - -val x = removeAll [1,2,3,3,4] 4; - -(* map question *) - -fun r li = map real li - -val res = r [1,2,3] \ No newline at end of file