Wednesday, 15 June 2011

Implementing collect for list in F# -



Implementing collect for list in F# -

i new programming in functional languages. attempting implement f# collect list.

let rec collect func list = match list | [] -> [] | hd::tl -> allow tlresult = collect func tl func hd::tlresult;; collect (fun x -> [for in 1..3 -> x * i]) [1;2;3];;

should print:

val : int list = [1; 2; 3; 2; 4; 6; 3; 6; 9]

but got:

val : int list = [[1; 2; 3;], [2; 4; 6;], [3; 6; 9]]

the collect function tricky implement efficiently in functional style, can quite implement using @ operator concatenates lists:

let rec collect f input = match input | [] -> [] | x::xs -> (f x) @ (collect f xs)

list f# functional-programming collect

No comments:

Post a Comment