From a3e7baf8690f91c8df376247945d6187eda4ea21 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 11 Oct 2024 11:36:24 -0700 Subject: [PATCH] Function currying (#5) * Currying functions quiz * Add comment re: param pattern --- .gitignore | 1 + src/quiz04-to-be-merged/quiz04.sml | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/quiz04-to-be-merged/quiz04.sml diff --git a/.gitignore b/.gitignore index c9a29a6..00e058c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ install-smlsharp links.md **/*temp.md todo.md +temp* # Logs logs diff --git a/src/quiz04-to-be-merged/quiz04.sml b/src/quiz04-to-be-merged/quiz04.sml new file mode 100644 index 0000000..b29f48d --- /dev/null +++ b/src/quiz04-to-be-merged/quiz04.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