fibnocci code in haskell language
Sure! Here's a Fibonacci code in Haskell:
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
This code defines a function fib
that takes an Int
parameter n
and returns the nth Fibonacci number. The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the previous two numbers.
The function uses pattern matching to handle the base cases where n
is 0 or 1. For any other value of n
, it recursively calls fib
on n-1
and n-2
and adds the results together.
You can call this function with an integer argument to calculate the corresponding Fibonacci number. For example, fib 5
would return 5, as the 5th Fibonacci number is 5.
I hope this helps! Let me know if you have any further questions.