Created
April 8, 2021 21:50
-
-
Save roehst/44b002632ae0489b3ddafcb8df56193e to your computer and use it in GitHub Desktop.
Fibonacci server with Dream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let state : (int * int) list ref = ref [] | |
let rec fibonacci (n : int) : int = | |
if n <= 1 then 1 | |
else | |
match List.assoc_opt n !state with | |
| Some v -> begin | |
print_string "Cache hit :)"; | |
print_newline (); | |
v | |
end | |
| None -> | |
let result = fibonacci (n - 1) + fibonacci (n - 2) in | |
state := (n, result) :: !state; | |
result | |
let () = | |
Dream.run @@ fun request -> | |
match Dream.query "n" request with | |
| Some message -> | |
let result = string_of_int @@ fibonacci @@ int_of_string message in | |
let count = List.length !state in | |
Dream.respond @@ "The fibonacci of " ^ message ^ " is " ^ result | |
^ " and memo has " ^ string_of_int count ^ " items" | |
| None -> Dream.respond "Please ask for a number" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment