In my opinion, Maybe
is too cumbersome here. I'd consider fib (-1)
to be ill-defined, and I'd let it just crash with an error
.
By your convention, the Fibonacci sequence is
1, 2, 3, 5, 8, 13, …
I would prefer to see it start earlier, such that fib 0
is 0:
0, 1, 1, 2, 3, 5, 8, 13, ….
The outputs can get pretty big. I suggest using Integer
instead of Int
.
You're using a very convoluted way to extract the nth item from a list.
Suggested solution
import Data.List (iterate)
fib :: Int -> Integer
fib n = fst $ sequence !! n
where
sequence = iterate (\(x, y) -> (y, x + y)) (0, 1)
You could also use the point-free style:
fib :: Int -> Integer
fib = (!!) $ map fst $ iterate (\(x, y) -> (y, x + y)) (0, 1)
For that matter, you could just define the infinite sequence
fibonacciSequence = map fst $ iterate (\(x, y) -> (y, x + y)) (0, 1)
… and let the caller do whatever they want with it, including extracting specific entries using !!
.
Behaviour
*Main> fib (-1)
*** Exception: Prelude.!!: negative index
*Main> fib 0
0
*Main> fib 1
1
*Main> fib 2
1
*Main> fib 3
2
*Main> fib 4
3
*Main> fib 5
5