List of entries in F #?

How do you work with a list of entries in F #? How could you pass this as an argument to a function? I want to do something like this:

type Car = { Color : string; Make : string; } let getRedCars cars = List.filter (fun x -> x.Color = "red") cars; let car1 = { Color = "red"; Make = "Toyota"; } let car2 = { Color = "black"; Make = "Ford"; } let cars = [ car1; car2; ] 

I need a way to tell my function that β€œcars” is a list of car records.

+4
source share
1 answer

Your code is working fine. It can also be written:

 let getRedCars cars = List.filter (function {Color = "red"} -> true | _ -> false) cars 

If you have ever worried that the wrong signature is displayed, you can add type annotations. For instance:

 let getRedCars (cars:Car list) : Car list = //... 
+6
source

All Articles