Search code examples
listfiltersml

How to use List.filter?


I have this code to filter list of string that the first letter is capital:

fun f s = Char.isUpper(String.sub(s,0));
fun only_capitals (xs : string list) =  List.filter(f , xs);

But when compile, I always receive error :

operator domain: 'Z -> bool
operand:         (string -> bool) * string list
  in expression:
    List.filter (f,xs)

What does this error mean? How to fix it?


Solution

  • Type signature of List.filter is

    val filter : ('a -> bool) -> 'a list -> 'a list
    

    So you need to give List.filter two distinct arguments, not one argument which happens to be a tuple.