Skip to content

Commit

Permalink
add fib.exe example
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Feb 10, 2024
1 parent fd5e82c commit f6db353
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

(executable
(name test)
(executables
(names test fib)
(libraries lwt lwt.unix))
27 changes: 27 additions & 0 deletions fib.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

open Lwt.Syntax

let suspend f =
let* () = Lwt.pause() in
f()

let rec sleepy_fib n =
if n <= 2 then (
Lwt.await @@ Lwt_unix.sleep 0.2;
Lwt.return 1
) else (
let f1 = sleepy_fib (n-1) in
let f2 = sleepy_fib (n-2) in
Lwt.await @@ Lwt_unix.sleep 0.2;
let res = Lwt.await f1 + Lwt.await f2 in
Printf.printf "fib %d = %d\n%!" n res;
Lwt.return res
)

let main () =
suspend @@ fun () ->
let _res = Lwt.await @@ sleepy_fib 10 in
Lwt.return()

let () =
Lwt_main.run @@ main ()

0 comments on commit f6db353

Please sign in to comment.