Skip to content

Commit

Permalink
Curry functions
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-byrne committed Oct 16, 2024
1 parent d5b8061 commit 8f04414
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 33 deletions.
26 changes: 26 additions & 0 deletions src/exam01/exam01.sml
Original file line number Diff line number Diff line change
@@ -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 *)
20 changes: 20 additions & 0 deletions src/quiz04/answers.sml
Original file line number Diff line number Diff line change
@@ -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];
27 changes: 13 additions & 14 deletions src/quiz04/quiz04.sml
Original file line number Diff line number Diff line change
@@ -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];
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]
19 changes: 0 additions & 19 deletions src/quiz04/quiz07.sml

This file was deleted.

0 comments on commit 8f04414

Please sign in to comment.