SML Pattern matching throwing "types of rules don't agree [tycon mismatch]" error -
SML Pattern matching throwing "types of rules don't agree [tycon mismatch]" error -
fun sample(list_of_str_lists, s) = case list_of_str_lists of [] => [] | x::[] => case (all_except_option(s, x)) of lst => lst | none => [] | x::xs' => case (all_except_option(s, x)) of lst => lst @ sample(xs', s) | none => [] @ sample(xs', s)
it uses helper function takes list of strings , returns elements in list in list option
if there matching string in list (again, excluding matching string). helper function take list ["a", "b", "c"]
, string "a"
, if matches "a"
, homecoming alternative containing ["b", “c”]
.
my problem in sample
in lastly pattern matching branch. throws error
earlier rule(s): 'z list alternative -> 'z list rule: string list list -> string list in rule: :: (x,xs') => (case (all_except_option (s,x)) of lst => lst @ sample <exp> | none => nil @ sample <exp>
but don’t understand why, because pattern i’m trying match calls some lst
it’s describing string list list
. what’s going on?
this syntax issue due nested case
expressions. parser assumes case | x::xs' => ...
continuation of ongoing case look case (all_except_option(s, x)) of ...
since has no way of telling it's meant belong enclosing look case list_of_str_lists of ...
.
if enclose inner case expressions in parentheses, function definition parse.
fun sample(list_of_str_lists, s) = case list_of_str_lists of [] => [] | x::[] => (case (all_except_option(s, x)) of lst => lst | none => []) | x::xs' => (case (all_except_option(s, x)) of lst => lst @ sample(xs', s) | none => [] @ sample(xs', s))
pattern-matching sml
Comments
Post a Comment