I'm trying to re-write a code using tail recursive local helper function as part of an assignment.
all_except_option is a function that has the return type fn : string * string list -> string list option
fun all_except_option ([], _ ) = NONE
| all_except_option (_, "") = NONE
| all_except_option (str_list, str) =
let
fun all_except_option' [] = []
| all_except_option' (x::str_list) =
if x = str then
all_except_option' str_list
else
x :: all_except_option' str_list
in
SOME (all_except_option' str_list)
end;
The function below is the one without using tail-recursive local helper function
fun sub1 ([], s) = []
| sub1 (x :: xs, s) =
case all_except_option(x, s) of NONE => sub1(xs, s) //
| SOME y => y @ get_substitutions1(xs, s);
This function use tail-recursion however I get an error where I'm calling the helper function recursively. The error is: Error: non-constructor applied to argument in pattern:all_except_option
fun get_substitutions2 (s,str) =
let fun aux(s,x::xs,acc) =
case x of [] => acc
| all_except_option(x, s) => aux(s,xs,xs::acc)
in
aux(s,str,[])
end;
xs::accwill fail asxsseem to be a list. - Diego Sevilla