sml - Type Mismatch in ML List.filter -
i trying write simple filter function in ml. idea function only_capitals
takes list of strings , returns list of strings, strings start capital letter. here implementation, getting type-error not understand:
fun only_capitals (strs : string list) = let fun iscapitalized (str) = char.isupper(string.sub(str, 0)) in list.filter(iscapital, strs) end
here error:
hw3provided.sml:5.18-5.27 error: unbound variable or constructor: iscapital hw3provided.sml:5.6-5.34 error: operator , operand don't agree [tycon mismatch] operator domain: 'z -> bool operand: _ * string list in expression: list.filter (<errorvar>,strs) val = () : unit
the first error caused typo; "iscapital" not name of function defined.
the second error looks strange because of first error – type _
refers type of iscapital
.
if fix first error, second should more like
error: operator , operand don't agree [tycon mismatch] operator domain: 'z -> bool operand: (string -> bool) * string list in expression: list.filter (iscapitalized,strs)
what compiler trying you're passing pair (iscapitalized,strs)
filter
expects function of type 'z -> bool
.
if look @ type of list.filter
, you'll notice ('a -> bool) -> 'a list -> 'a list
– it's curried function.
what should write is
fun only_capitals (strs : string list) = let fun iscapitalized (str) = char.isupper(string.sub(str, 0)) in list.filter iscapitalized strs end
Comments
Post a Comment