Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 266725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:28:05+00:00 2026-05-11T23:28:05+00:00

In Haskell, how can I generate Fibonacci numbers based on the property that the

  • 0

In Haskell, how can I generate Fibonacci numbers based on the property that the nth Fibonacci number is equal to the (n-2)th Fibonacci number plus the (n-1)th Fibonacci number?

I’ve seen this:

fibs :: [Integer]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

I don’t really understand that, or how it produces an infinite list instead of one containing 3 elements.

How would I write haskell code that works by calculating the actual definition and not by doing something really weird with list functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T23:28:05+00:00Added an answer on May 11, 2026 at 11:28 pm

    Here’s a different and simpler function that calculates the n’th Fibonacci number:

    fib :: Integer -> Integer
    fib 0 = 0
    fib 1 = 1
    fib n = fib (n-1) + fib (n-2)
    

    The implementation you are referring to relays on some observations about how values in Fibonacci relate to each other (and how Haskell can define data structures in terms of themselfs in effect creating infinite data structures)

    The function in your question works like this:

    Assume you already had an infinite list of the Fibonacci numbers:

       [ 1, 1, 2, 3, 5,  8, 13, .... ]
    

    The tail of this list is

       [ 1, 2, 3, 5, 8, 13, 21, .... ]
    

    zipWith combines two lists element by element using the given operator:

       [ 1, 1, 2, 3,  5,  8, 13, .... ]
    +  [ 1, 2, 3, 5,  8, 13, 21, .... ]
    =  [ 2, 3, 5, 8, 13, 21, 34, .... ]
    

    So the infinite list of Fibonacci numbers can be calculated by prepending the elements 1 and 1 to the result of zipping the infinite list of Fibonacci numbers with the tail of the infinite list of Fibonacci numbers using the + operator.

    Now, to get the n’th Fibonacci number, just get the n’th element of the infinite list of Fibonacci numbers:

    fib n = fibs !! n
    

    The beauty of Haskell is that it doesn’t calculate any element of the list of Fibonacci numbers until its needed.

    Did I make your head explode? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I open a file for reading in Haskell, I've found that I can't
I am new to Haskell and I wonder how/if I can make this code
According to a number of tutorials (including Real World Haskell) one can, using ghci
I am trying to use the Data.Unique in Haskell. The newUnique can generate an
Let's say I want to generate a random number in Haskell. To do so,
Possible Duplicate: Split a number into its digits with Haskell how can i convert
I'm trying to understand type patterns and generic classes in Haskell but can't seem
How can I do an HTTPS request in Haskell? For example, I want to
Can 2 or more equations defining a function in Haskell share the same where
Haskell provides the feature something like f = f1 . f2 How can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.