- 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
and I'm thinking the F# code should look something like
but obviously something is wrong with the syntax and I find F# error messages rather cryptic. Any help would be appreciated.
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