Skip to content

Instantly share code, notes, and snippets.

@wklm wklm/fib.fs
Last active Sep 24, 2017

Embed
What would you like to do?
fibonacci async
let rec fib = function
| 0L | 1L as n -> n
| n -> (fib (n - 1L) + fib (n - 2L))
let memoize f =
let memo = ref Map.empty
fun arg ->
if Map.containsKey arg !memo then Map.find arg !memo
else
let result = f arg
memo := Map.add arg result !memo
result
let rec fibm =
memoize <| fun n ->
match n with
| 0L | 1L as n -> n
| n -> fibm (n - 1L) + fibm (n - 2L)
let go (f : int64 -> int64) (range : int64) =
seq { for i in [0L..range] do yield async {return f i} }
|> Async.Parallel
|> Async.RunSynchronously
[<EntryPoint>]
let main argv =
let res = match argv.[0] |> string with
| "fib" -> fib (int64 argv.[1])
| "fibm" -> fibm (int64 argv.[1])
| _ -> -1L
printfn "%i" res
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.