Mathematicaand maybe other languages have a function foldList. This is very similar to fold, but instead of returning only the final calculated value, it returns each intermediate value.
It is easy to write a function foldListin F #:
let foldList f (x: 'S) (m: list<'T>) =
let fs (xs: list<'S>) (y: 'T) = (f (Seq.head xs) y)::xs
List.fold fs [x] m
|> List.rev
let m = [1; 2; -3; 5]
foldList (+) 0 m
List.fold (+) 0 m
Is there such a function in F #? If not, is there a more efficient implementation than the one above? Is there a way to avoid calling List.rev?
source
share