Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Effect
- open Effect.Shallow
- type 'a t += Recur : 'a -> 'b t
- let recur a = perform (Recur a)
- let loop : 'a -> ('a -> 'b) -> 'b =
- fun init fn ->
- let rec run : ('a, 'b) continuation -> 'a -> 'b =
- fun k input ->
- continue_with k input
- {
- retc = (fun x -> x);
- exnc = (fun e -> raise e);
- effc =
- (fun (type b) (eff : b t) ->
- match eff with
- | Recur value ->
- Some
- (fun (_ : (b, _) continuation) ->
- run (fiber fn) (Obj.magic value))
- | _ -> None);
- }
- in
- run (fiber fn) init
- let fact i =
- loop (i, 1) (fun (iter, acc) ->
- if iter > 0 then recur (iter - 1, iter * acc, 1) else acc)
- let () = print_int (fact 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement