Aminion
Aminion

Reputation: 465

Printing a list of records in F#

I'm new in F# and I don't understand next error:

let g = 9.8
let v = 320.0
let a = 45.0
let iterations = List.unfold(fun x-> if x < 100.0 then Some(x,x+0.1) else None) 0.0
type point = {x:float;y:float}
let tPoints = List.map(fun t -> {x=v*t*cos(a); y=v*t*sin(a)-g*(t**2.0);})
for point in tPoints do
    printfn "%A" point

at line:

for point in tPoints do

The type '(float list -> point list)' is not a type whose values can be enumerated with this syntax, i.e. is not compatible with either seq<>, IEnumerable<> or IEnumerable and does not have a GetEnumerator method fs

tPoints|>List.iter(fun p->printfn "%A" p)

in this way it's also doesn't work

Upvotes: 3

Views: 762

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80744

tPoints is not a list, but a function.

List.map takes two arguments - a function and a list, - and returns a list. You only gave it one argument, so the return value is a function that is still "waiting" for the second argument.

The type of that function is (float list -> point list) - that is, it expects an argument of type float list and will return a point list. You are trying to iterate over that function, and this is what the compiler tells you that you can't do: cannot enumerate values of type float list -> point list.

I guess you meant to pass iterations to it:

let tPoints = List.map(fun t -> {x=v*t*cos(a); y=v*t*sin(a)-g*(t**2.0);}) iterations

Upvotes: 3

Related Questions