Guest User

Untitled

a guest
Aug 4th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.75 KB | None | 0 0
  1. (* Fibonacci Number formula *)
  2. let rec fib n =
  3.     match n with
  4.     | 0 | 1 -> n
  5.     | _ -> fib (n - 1) + fib (n - 2)
  6.  
  7. (* An alternative approach - a lazy recursive sequence of Fibonacci numbers *)
  8. let rec fibs = Seq.cache <| seq { yield! [1; 1]                                  
  9.                                   for x, y in Seq.zip fibs <| Seq.skip 1 fibs -> x + y }
  10.  
  11. (* Another approach - a lazy infinite sequence of Fibonacci numbers *)
  12. let fibSeq = Seq.unfold (fun (a,b) -> Some(a+b, (b, a+b))) (1,1)
  13.  
  14. (* Print even fibs *)
  15. [1 .. 10]
  16. |> List.map     fib
  17. |> List.filter  (fun n -> (n % 2) = 0)
  18. |> printlist
  19.  
  20. (* Same thing, using sequence expressions *)
  21. [ for i in 1..10 do
  22.     let r = fib i
  23.     if r % 2 = 0 then yield r ]
  24. |> printlist
Add Comment
Please, Sign In to add comment