This problem involves using first-name substitutions to come up with alternate n
ID: 3542355 • Letter: T
Question
This problem involves using first-name substitutions to come up with alternate names. For example,
Do not use functions null,hd, tl, isSome, or valOf, or nor # character.
Write a function all_except_option, which takes a string and a string list. Return NONE if the string is not in the list, else return SOME lst where lst is identical to the argument list except the string is not in it. You may assume the string is in the list at most once. Use same_string, provided to you, to compare strings. Sample solution is around 8 lines.
binding generated:
val all_except_option = fn : string * string list -> string list option
Explanation / Answer
fun filter (element : string, list : string list) = case list of [] => [] | head::tail => let val a = filter(element, tail) in if same_string(head, element) then a else head::a end (* check if an element exists in the given list *) fun exist_in_list (element : string, list : string list) = case list of [] => false | head::tail => if same_string(element, head) then true else exist_in_list(element, tail)fun all_except_option (element : string, list : string list) = if exist_in_list(element, list) then SOME (filter(element, list)) else NONE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.