user783836
user783836

Reputation: 3499

F# can't invoke Seq.fold without piping?

I'm busy learning F# and was playing around with Seq.fold. Could anyone explain why the following two invocations are not essentially the same and one errors and the other does not.

Invoking this way:

Seq.fold (fun state input -> state + input) 0 Seq.ofList [1;2;3;4;5];;

Results in the following error:

error FS0001: This expression was expected to have type
    ''a -> 'b'
but here has type
    'int'

Invoking with piping works fine:

Seq.ofList [1;2;3;4;5] |> Seq.fold (fun state input -> state + input) 0;;

I'm guessing I've somehow taken a generic function and forced it to be int only.

Upvotes: 2

Views: 160

Answers (2)

gileCAD
gileCAD

Reputation: 2493

Seq.ofList is not mandatory. You can directly write:

Seq.fold (fun state input -> state + input) 0 [1;2;3;4;5]

or:

[1;2;3;4;5] |> Seq.fold (fun state input -> state + input) 0 

Upvotes: 3

Foole
Foole

Reputation: 4850

You're passing Seq.ofList as the 3rd parameter to Seq.fold. You need to add some parens:

Seq.fold (fun state input -> state + input) 0 (Seq.ofList [1;2;3;4;5]);;

Upvotes: 7

Related Questions