up to 8 in week4
This commit is contained in:
parent
f6e82f5b80
commit
a43a8271b3
3 changed files with 69 additions and 1 deletions
|
@ -1,6 +1,8 @@
|
||||||
use "operators.sml";
|
use "operators.sml";
|
||||||
use "list.sml";
|
use "list.sml";
|
||||||
|
|
||||||
|
exception NoAnswer
|
||||||
|
|
||||||
fun first str = String.sub (str, 0)
|
fun first str = String.sub (str, 0)
|
||||||
val only_capitals = filter (first >> Char.isUpper)
|
val only_capitals = filter (first >> Char.isUpper)
|
||||||
|
|
||||||
|
@ -26,3 +28,24 @@ fun longest_string_helper f =
|
||||||
val longest_capitalized = only_capitals >> longest_string2
|
val longest_capitalized = only_capitals >> longest_string2
|
||||||
|
|
||||||
val rev_string = String.explode >> List.rev >> String.implode
|
val rev_string = String.explode >> List.rev >> String.implode
|
||||||
|
|
||||||
|
fun first_answer f lst =
|
||||||
|
case lst of
|
||||||
|
[] => raise NoAnswer
|
||||||
|
| head :: tail =>
|
||||||
|
case f head of
|
||||||
|
NONE => first_answer f tail
|
||||||
|
| SOME answer => answer
|
||||||
|
|
||||||
|
fun all_answers (f: ('a -> 'b list option)) lst: 'b list option =
|
||||||
|
let
|
||||||
|
fun collect lst acc =
|
||||||
|
case lst of
|
||||||
|
[] => SOME acc
|
||||||
|
| head :: tail =>
|
||||||
|
case f head of
|
||||||
|
NONE => NONE
|
||||||
|
| SOME lst => collect tail (acc @ lst)
|
||||||
|
in
|
||||||
|
collect lst []
|
||||||
|
end
|
|
@ -44,5 +44,47 @@ val () =
|
||||||
$ rev_string "string" = "gnirts"
|
$ rev_string "string" = "gnirts"
|
||||||
$ "rev_string: reverses string"
|
$ "rev_string: reverses string"
|
||||||
|
|
||||||
|
val NONE =
|
||||||
|
first_answer (fn elm => NONE) []
|
||||||
|
handle NoAnswer =>
|
||||||
|
assert true "first_answer: rises on on empty lst"
|
||||||
|
|> (fn _ => NONE)
|
||||||
|
|
||||||
|
val NONE =
|
||||||
|
first_answer (fn elm => NONE) ["elm"]
|
||||||
|
handle NoAnswer =>
|
||||||
|
assert true "first_answer: rises on miss"
|
||||||
|
|> (fn _ => NONE)
|
||||||
|
|
||||||
|
val () =
|
||||||
|
assert
|
||||||
|
$ first_answer (fn elm => SOME elm) ["elm"] = "elm"
|
||||||
|
$ "first_answer: returns first answer"
|
||||||
|
|
||||||
|
|
||||||
|
val () =
|
||||||
|
assert
|
||||||
|
$ first_answer
|
||||||
|
(fn elm => if elm = "second" then SOME elm else NONE)
|
||||||
|
["elm", "second"] = "second"
|
||||||
|
$ "first_answer: returns some answer"
|
||||||
|
|
||||||
|
val () =
|
||||||
|
assert
|
||||||
|
$ all_answers (fn elm => SOME elm) [] = SOME []
|
||||||
|
$ "all_answers: returns SOME [] on []"
|
||||||
|
|
||||||
|
val () =
|
||||||
|
assert
|
||||||
|
$ all_answers (fn elm => SOME [elm]) ["a", "b"] = SOME ["a", "b"]
|
||||||
|
$ "all_answers: returns all answers"
|
||||||
|
|
||||||
|
val () =
|
||||||
|
assert
|
||||||
|
$ all_answers
|
||||||
|
(fn elm => if elm = "a" then SOME [elm] else NONE)
|
||||||
|
["a", "b"] = NONE
|
||||||
|
$ "all_answers: NONE on at least one NONE"
|
||||||
|
|
||||||
|
|
||||||
val () = complete ()
|
val () = complete ()
|
|
@ -2,6 +2,8 @@ use "operators.sml";
|
||||||
|
|
||||||
fun cons head tail = head :: tail
|
fun cons head tail = head :: tail
|
||||||
|
|
||||||
|
fun append a b = a @ b
|
||||||
|
|
||||||
fun foldl f acc lst =
|
fun foldl f acc lst =
|
||||||
case lst of
|
case lst of
|
||||||
[] => acc
|
[] => acc
|
||||||
|
@ -9,6 +11,7 @@ fun foldl f acc lst =
|
||||||
|
|
||||||
fun reverse lst = foldl cons [] lst
|
fun reverse lst = foldl cons [] lst
|
||||||
|
|
||||||
|
(* wrong implementation *)
|
||||||
fun foldr f acc = foldl f acc >> reverse
|
fun foldr f acc = foldl f acc >> reverse
|
||||||
|
|
||||||
fun map f = foldr (f >> cons) []
|
fun map f = foldr (f >> cons) []
|
||||||
|
|
Loading…
Reference in a new issue