Dec 7th, 2021
531
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. module Fib
2.     (
3.         simpleRecursionFibs,
4.         tailRecursionFibs,
5.         byZipWithFibs,
6.         simpleIterateFibs
7.     ) where
8.
9. {-
10.     fibs by usual recursion
11. -}
12.
13. simpleRecursionFibs :: Int -> [Integer]
14. simpleRecursionFibs n = map simpleRecursionFib [1 .. n]
15.
16. simpleRecursionFib :: Int -> Integer
17. simpleRecursionFib 0 = 1
18. simpleRecursionFib 1 = 1
19. simpleRecursionFib n = simpleRecursionFib (n - 1) + simpleRecursionFib (n - 2)
20.
21. {-
22.     fibs by tail recursion
23. -}
24.
25. tailRecursionFibs :: Int -> [Integer]
26. tailRecursionFibs n = map tailRecursionFib [1 .. n]
27.
28. fibsTailTriple :: (Int, Integer, Integer) -> Int -> (Int, Integer, Integer)
29. fibsTailTriple (curN, x, y) n =
30.     if curN < n
31.     then fibsTailTriple (curN + 1, y, x + y) n
32.     else (curN, x, y)
33.
34. third :: (x,y,z) -> z
35. third (x,y,z) = z
36.
37. tailRecursionFib :: Int -> Integer
38. tailRecursionFib n = third (fibsTailTriple (0,0,1) n)
39.
40. {-
41.     by zipWith
42. -}
43.
44. byZipWithFibs :: Int -> [Integer]
45. byZipWithFibs n = take n fibsByZipWith
46.
47. fibsByZipWith = 1 : 2 : zipWith (+) fibsByZipWith (tail fibsByZipWith)
48.
49. {-
50.     simple iterate fibs
51. -}
52.
53. simpleIterateFibs :: Int -> [Integer]
54. simpleIterateFibs n = take n simpleIterateFib
55.
56. simpleIterateFib = map fst (iterate f (1, 2)) where f (x, y) = (y, x + y)
57.
58.
RAW Paste Data