haskell fibonacci
Haskell Fibonacci
To calculate the Fibonacci sequence in Haskell, you can use recursion. Here's an example of how you can implement it:
fibonacci :: Int -> Int
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci (n-1) + fibonacci (n-2)
In this code, the fibonacci
function takes an integer n
as input and returns the n
th Fibonacci number. The base cases are defined for 0
and 1
, where the Fibonacci numbers are 0
and 1
respectively. For any other value of n
, the function recursively calls itself with n-1
and n-2
as arguments, and adds the results together.
You can test the fibonacci
function by calling it with different values of n
. For example, fibonacci 5
will return 5
, as the 5th Fibonacci number is 5
.
I hope this helps! Let me know if you have any further questions.