Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use List.filter?

Tags:

list

filter

sml

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?

like image 459
hqt Avatar asked Feb 06 '13 10:02

hqt


People also ask

How do you use list filters?

You can use the FILTER and COUNTIF functions to filter based on a list in Excel. To filter by a list in Excel, use the COUNTIF function to give an indication of whether or not each row meets your criteria, and then use the FILTER function to filter out the rows that do not meet your criteria.

What is a list filter in Python?

filter(fn, list) Code language: Python (python) The filter() function iterates over the elements of the list and applies the fn() function to each element. It returns an iterator for the elements where the fn() returns True .

How do you filter a list in a list Python?

Short answer: To filter a list of lists for a condition on the inner lists, use the list comprehension statement [x for x in list if condition(x)] and replace condition(x) with your filtering condition that returns True to include inner list x , and False otherwise.


1 Answers

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.

like image 153
pad Avatar answered Feb 17 '23 09:02

pad