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 6001161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:47:46+00:00 2026-05-23T00:47:46+00:00

I’m learning Haskell, and came across the following code: fibs = 0 : 1

  • 0

I’m learning Haskell, and came across the following code:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

which I’m having a bit of trouble parsing, in terms of how it works. It’s very neat, I understand that nothing more is needed, but I’d like to understand how Haskell manages to “fill in” fibs when I write:

take 50 fibs

Any help?

Thanks!

  • 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-23T00:47:46+00:00Added an answer on May 23, 2026 at 12:47 am

    I’ll give a bit of an explanation of how it works internally. First, you must realise that Haskell uses a thing called a thunk for its values. A thunk is basically a value that has not yet been computed yet — think of it as a function of 0 arguments. Whenever Haskell wants to, it can evaluate (or partly-evaluate) the thunk, turning it in to a real value. If it only partly evaluates a thunk, then the resulting value will have more thunks in it.

    For example, consider the expression:

    (2 + 3, 4)
    

    In an ordinary language, this value would be stored in memory as (5, 4), but in Haskell, it is stored as (<thunk 2 + 3>, 4). If you ask for the second element of that tuple, it will tell you “4”, without ever adding 2 and 3 together. Only if you ask for the first element of that tuple will it evaluate the thunk, and realise that it is 5.

    With fibs, it’s a bit more complicated, because it’s recursive, but we can use the same idea. Because fibs takes no arguments, Haskell will permanently store any list elements that have been discovered — that is important.

    fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
    

    It helps to visualise Haskell’s current knowledge of three expressions: fibs, tail fibs and zipWith (+) fibs (tail fibs). We shall assume Haskell starts out knowing the following:

    fibs                         = 0 : 1 : <thunk1>
    tail fibs                    = 1 : <thunk1>
    zipWith (+) fibs (tail fibs) = <thunk1>
    

    Note that the 2nd row is just the first one shifted left, and the 3rd row is the first two rows summed.

    Ask for take 2 fibs and you’ll get [0, 1]. Haskell doesn’t need to further evaluate the above to find this out.

    Ask for take 3 fibs and Haskell will get the 0 and 1, and then realise that it needs to partly evaluate the thunk. In order to fully evaluate zipWith (+) fibs (tail fibs), it needs to sum the first two rows — it can’t fully do that, but it can begin to sum the first two rows:

    fibs                         = 0 : 1 : 1: <thunk2>
    tail fibs                    = 1 : 1 : <thunk2>
    zipWith (+) fibs (tail fibs) = 1 : <thunk2>
    

    Note that I filled in the “1” in the 3rd row, and it automatically appeared in the first and second rows as well, since all three rows are sharing the same thunk (think of it like a pointer that got written to). And because it didn’t finish evaluating, it created a new thunk containing the rest of the list, should that ever be needed.

    It isn’t needed, though, because take 3 fibs is done: [0, 1, 1]. But now, say you ask for take 50 fibs; Haskell already remembers the 0, 1 and 1. But it needs to keep going. So it continues summing the first two rows:

    fibs                         = 0 : 1 : 1 : 2 : <thunk3>
    tail fibs                    = 1 : 1 : 2 : <thunk3>
    zipWith (+) fibs (tail fibs) = 1 : 2 : <thunk3>
    

    …

    fibs                         = 0 : 1 : 1 : 2 : 3 : <thunk4>
    tail fibs                    = 1 : 1 : 2 : 3 : <thunk4>
    zipWith (+) fibs (tail fibs) = 1 : 2 : 3 : <thunk4>
    

    And so on, until it has filled in 48 columns of the 3rd row, and thus has worked out the first 50 numbers. Haskell evaluates just as much as it needs, and leaves the infinite “rest” of the sequence as a thunk object in case it ever needs any more.

    Note that if you subsequently ask for take 25 fibs, Haskell will not evaluate it again — it will just take the first 25 numbers from the list it has already calculated.

    Edit: Added a unique number to each thunk to avoid confusion.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.