Haskell Program to Display Fibonacci Series


This tutorial discusses writing a program to display Fibonacci Series in the Haskell programming language.

The Fibonacci series is a sequence in which each number is the sum of the preceding two terms. The Fibonacci series up to five terms is 0 1 1 2 3.

In this tutorial we see,

  • Program to print nth fibonacci number.
  • Program to print the first ‘n’ terms in the Fibonacci series.
  • Program to print the first ‘n’ terms in the Fibonacci series using the list comprehension.
  • Program to print the first ‘n’ terms in the Fibonacci series using the map function.

Algorithmic Steps

  • Take input a required range value n.
  • Implement the program logic to print the Fibonacci series.

Example 1

Program to print the nth Fibonacci number.

-- function declaration
getNthFibo :: Int->Int

-- function definition
-- base case-1
getNthFibo 1 = 0
-- base case-2
getNthFibo 2 = 1
getNthFibo n = getNthFibo (n-1) + getNthFibo (n-2)

main :: IO()
main = do
-- declaring and initializing variable n
   let n = 11
-- invoking the function getNthFibo and printing the returned output
   print (show n ++ "th Fibonacci number is:")
   print (getNthFibo n)

Output

"11th Fibonacci number is:"
55

In the above program, we declared a function getNthFibo as such it takes an integer as argument and returns an integer. In its function definition, we are taking an argument n and returning the addition of recursive calls to itself with arguments (n-1) and (n-2). The recursive calls are invoked until the base case is reached, i.e when the argument is 0 the function returns the value 0 and returns the value 1 when the argument is 1. The program prints the nth Fibonacci number. In the main function, we declared and initialized a variable n. We invoked the function getNthFibo and printed the returned integer.

Example 2

Program to display the first ‘n’ terms in the Fibonacci series.

-- function declaration
getNthFibo :: Int->Int

-- function definition
-- base case-1
getNthFibo 1 = 0
-- base case-2
getNthFibo 2 = 1
getNthFibo n = getNthFibo (n-1) + getNthFibo (n-2)

-- function declaration
getFiboSeries :: Int -> [Int]
-- function definition
-- base case
getFiboSeries 0 = []
getFiboSeries n = getFiboSeries (n-1)++[getNthFibo n]

main :: IO()
main = do
-- declaring and initializing variable n
    let n = 11
-- invoking the function getFiboSeries and printing the returned output
    print ("The first "++ show n ++ " terms in the Fibonacci series are:")
    print (getFiboSeries n)

Output

"The first 11 terms in the Fibonacci series are:"
[0,1,1,2,3,5,8,13,21,34,55]

In the above program, we implemented a function getNthFibo which returns the nth Fibonacci number as in the previous example. We declared a function getFiboSeries that takes an integer as an argument and returns a list of integers. In its function definition, it takes an argument n and returns a recursive call contacted with a list containing a single element, an nth Fibonacci number. The recursive calls are invoked until the function attains base case i.e the argument is value 0. In this case, the function returns an empty list. This function returns the list of the first n Fibonacci series. In the main function, we declared and initialized a variable for n. We invoked the function getFiboSeries and printed the returned integer list.

Example 3

Program to display the first ‘n’ terms in the Fibonacci series using list comprehension.

-- function declaration
getNthFibo :: Int->Int

-- function definition
-- base case-1
getNthFibo 1 = 0
-- base case-2
getNthFibo 2 = 1
getNthFibo n = getNthFibo (n-1) + getNthFibo (n-2)

main :: IO()
main = do
-- declaring and initializing variable n
   let n = 11
   let fiboSeries = [getNthFibo x | x<-[1..n]]
   print ("The first "++ show n ++ " terms in the Fibonacci series are:")
   print (fiboSeries)

Output

"The first 11 terms in the Fibonacci series are:"
[0,1,1,2,3,5,8,13,21,34,55]

In the above program, we declared and initialized a variable n. We used list comprehension to generate a list of integers from 1 to n and return the output of the function getNthFibo with the element as an argument. We loaded the list into a variable fiboSeries.The function getNthFibo returns the nth Fibonacci number. Finally, we printed the list.

Example 4

Program to display the first ‘n’ terms in the Fibonacci series using the map function.

-- function declaration
getNthFibo :: Int->Int

-- function definition
-- base case-1
getNthFibo 1 = 0
-- base case-2
getNthFibo 2 = 1
getNthFibo n = getNthFibo (n-1) + getNthFibo (n-2)

main :: IO()
main = do
-- declaring and initializing variable n
   let n = 11
   let fiboSeries = map getNthFibo [1..n]
   print ("The first "++ show n ++ " terms in the Fibonacci series are:")
   print (fiboSeries)

Output

"The first 11 terms in the Fibonacci series are:"
[0,1,1,2,3,5,8,13,21,34,55]

In the above program,  we declared and initialized a variable n. We used the map function to execute the function getNthFibo on a list. The function map takes a function and a list as arguments and returns a list whose elements are the returned elements for the function executed on list elements. The function getNthFibo is a function that returns the nth Fibonacci number.

Conclusion

In this tutorial, we discussed four different ways to implement a program to display the Fibonacci series in the Haskell programming language.

Updated on: 14-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements