Help with F#, filtering a list using two disjunct conditions?

Oct 27, 2007
17,009
5
0
I'm trying to learn F# and I have a very simple problem I want to solve - I have a list of integers and I want to filter it so that only numbers which meet at least one of two conditions remain. This seems really freaking simple but I can't figure out how to do it in F#.

Let's say I want to keep a list of the numbers that are evenly divisible by either 2 or 3. In C# or similar I'd write

Code:
List<int> filterList(List<int> list) {
  List<int> result = new ArrayList<int>();
  foreach (int i in list)
    if (i % 2 == 0 || i % 3 == 0) result.add(i);
  return result;
}

and I'm thinking the F# code should look something like
Code:
let filterList list = List.filter (fun n%3=0 || n%2=0) list
but obviously something is wrong with the syntax and I find F# error messages rather cryptic. Any help would be appreciated.
 

TheRickRoller

Member
Dec 2, 2009
164
0
0
Code:
let filterList list = List.filter (fun n%3=0 || n%2=0) list

Hey friend, I am learning F# right now too. When creating a lambda, you need to use -> like this:

Code:
let filterList list = List.filter (fun n -> n%3=0 || n%2=0) list


Otherwise you're on the right track.