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

  • Home
  • SEARCH
  • 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 7823719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:21:00+00:00 2026-06-02T08:21:00+00:00

Ok basically I have a problem knowing whether option 1 or 2 applies in

  • 0

Ok basically I have a problem knowing whether option 1 or 2 applies in the following case:

naturals = 0 : map (+ 1) naturals

Where options are:
1. The execution is awful, everything is recomputed at each step:

naturals     = [0]
naturals'    = 0:map (+ 1) [0]          // == [0, 1]
naturals''   = 0:map (+ 1) [0, 1]       // == [0, 1, 2]
naturals'''  = 0:map (+ 1) [0, 1, 2]    // == [0, 1, 2, 3]
naturals'''' = 0:map (+ 1) [0, 1, 2, 3] // == [0, 1, 2, 3, 4]

2. The execution is not awful, the list is always infinite and map is applied once only

naturals     = 0:something
                                  |
naturals'    = 0:      map (+ 1) (0:      something)
                                    |
naturals''   = 0:1:    map (+ 1) (0:1:    something')
                                      |
naturals'''  = 0:1:2:  map (+ 1) (0:1:2:  something'')
                                        |
naturals'''' = 0:1:2:3:map (+ 1) (0:1:2:3:something''')

with | indicating where map is at in its execution.

I do know that answers may be only 1 or 2 but I’d appreciate some pointers to good explanations on co-recursion too to clear the last doubts 🙂

  • 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-06-02T08:21:04+00:00Added an answer on June 2, 2026 at 8:21 am

    Execution isn’t going to be, as you put it, “awful”. 🙂 Lazy evaluation is your best friend here. What does laziness mean?

    1. Things are not evaluated before their results are really needed;
    2. Things are evaluated at most once.

    “Things”, here, are “not-yet-evaluated expressions”, also known as “thunks”.

    Here is what happens:

    You define

    naturals = 0 : map (+1) naturals
    

    Merely defining naturals doesn’t introduce a need to evaluate it, so initially naturals will just point to a thunk for the unevaluated expression 0 : map (+1) naturals:

    naturals = <thunk0>
    

    At some point, your program may pattern match on naturals. (Pattern matching is essentially the only thing that forces evaluation in a Haskell program.) That is, your program needs to know whether naturals is the empty list or a head element followed by a tail list. This is where the right-hand side of your definition will be evaluated, but only as far as needed to find out whether naturals is constructed by [] or (:):

    naturals = 0 : <thunk1>
    

    That is naturals will now point to an application of the constructor (:) on the head element 0 and a thunk for the still-unevaluated tail. (Actually, the head element will also be still-unevaluated, so really naturals will point to something of the form <thunk> : <thunk>, but I will be leaving that detail out.)

    It is not until some later point in your program where you may pattern match on the tail that the thunk for the tail gets “forced”, i.e., evaluated. This means that the expression map (+1) naturals is to be evaluated. Evaluating this expression reduces to map pattern matching on naturals: it needs to know whether naturals is constructed by [] or (:).
    We saw that, at this point, rather than pointing to a thunk, naturals is already pointing to an application of (:), so this pattern match by map requires no further evaluation. The application of map does immediately see enough of naturals to figure out that it needs to produce an application of (:) itself and so it does: map produces 1 : <thunk2> where the thunk contains an unevaluated expression of the form map (+1) <?>. (Again, instead of 1, we actually have a thunk for 0 + 1.) What is <?> pointing to? Well, the tail of naturals, which happens to be what map was producing. Hence, we now have

    naturals = 0 : 1 : <thunk2>
    

    with <thunk2> containing the yet-unevaluated expression map (+1) (1 : <thunk2>).

    At yet a later point in your program, pattern matching may force <thunk2>, so that we get

    naturals = 0 : 1 : 2 : <thunk3>
    

    with <thunk3> containing the yet-unevaluated expression map (+1) (2 : <thunk3>). And so forth.

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

Sidebar

Related Questions

can anyone help? I have a problem aligning rounded corners in IE6/7. Basically everything
I have the following problem Basically I have the 2 snippets below: var contactAssociation
I have a problem trying to design some generic storage.. Basically I have the
I have basically the same problem outlined in this question, however I am using
I have a problem with my BroadcastReceiver. Basically, I have a function in my
I have a pretty simple problem. Basically I have an array called $list that
I'm not finding any question specifically with the odd problem I have. Basically I've
This is basically a math problem, but very programing related: if I have 1
The problem is basically this, in python's gobject and gtk bindings. Assume we have
I basically have the same problem in this questions: Flash Video still playing in

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.