1 people like it.
Like the snippet!
Successive fold functions
fold by applying a list of function to a list of arg, last one behaving as usual
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
|
let sa = [ 1 .. 10 ]
let rec fold f s xs =
match xs with
| [] -> s
| x::xs -> fold f (f s x) xs
fold (+) 0 sa
let rec foldps f s xs k =
match xs with
| [] -> k s
| x::xs -> foldps f s xs (fun s -> f x s k)
let pluscps = (fun x s cont -> cont(s + x))
foldps pluscps 0 sa id
let rec foldpsn fs s xs k =
match xs with
| [] -> k s
| x::xs -> match fs with
| f::[] -> foldps f s xs (fun s -> f x s k)
| f::fs -> foldpsn fs s xs (fun s -> f x s k)
| _ -> failwith "not good"
let printcps = (fun x s cont -> printfn "%A" x;cont(s))
foldpsn [printcps;pluscps] 0 sa id
|
val sa : int list
Full name: Script.sa
val fold : f:('a -> 'b -> 'a) -> s:'a -> xs:'b list -> 'a
Full name: Script.fold
val f : ('a -> 'b -> 'a)
val s : 'a
val xs : 'b list
val x : 'b
val foldps : f:('a -> 'b -> ('b -> 'c) -> 'c) -> s:'b -> xs:'a list -> k:('b -> 'c) -> 'c
Full name: Script.foldps
val f : ('a -> 'b -> ('b -> 'c) -> 'c)
val s : 'b
val xs : 'a list
val k : ('b -> 'c)
val x : 'a
val pluscps : x:int -> s:int -> cont:(int -> 'a) -> 'a
Full name: Script.pluscps
val x : int
val s : int
val cont : (int -> 'a)
val id : x:'T -> 'T
Full name: Microsoft.FSharp.Core.Operators.id
val foldpsn : fs:('a -> 'b -> ('b -> 'c) -> 'c) list -> s:'b -> xs:'a list -> k:('b -> 'c) -> 'c
Full name: Script.foldpsn
val fs : ('a -> 'b -> ('b -> 'c) -> 'c) list
val failwith : message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.failwith
val printcps : x:'a -> s:'b -> cont:('b -> 'c) -> 'c
Full name: Script.printcps
val cont : ('b -> 'c)
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
More information