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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:21:11+00:00 2026-05-27T02:21:11+00:00

If so, is this a part of the standard or a ghc specific optimisation

  • 0

If so, is this a part of the standard or a ghc specific optimisation we can depend on? Or just an optimisation which we can’t necessarily depend on.

P.S.:
When I tried a test sample, it seemed to indicate that it was taking place/

Prelude> let isOdd x = x `mod` 2 == 1
Prelude> let isEven x = x `mod` 2 == 0
Prelude> ((filter isOdd).(filter isEven)) [1..]

Chews up CPU but doesn’t consume much memory.

  • 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-27T02:21:11+00:00Added an answer on May 27, 2026 at 2:21 am

    Depends on what you mean by generator. The list is lazily generated, and since nothing else references it, the consumed parts are garbage collected almost immediately. Since the result of the above computation doesn’t grow, the entire computation runs in constant space. That is not mandated by the standard, but as it is harder to implement nonstrict semantics with different space behaviour for that example (and lots of vaguely similar), in practice you can rely on it.

    But normally, the list is still generated as a list, so there’s a lot of garbage produced. Under favourable circumstances, ghc eliminates the list [1 .. ] and produces a non-allocating loop:

    result :: [Int]
    result = filter odd . filter even $ [1 .. ]
    

    (using the Prelude functions out of laziness), compiled with -O2 generates the core

    List.result_go =
      \ (x_ayH :: GHC.Prim.Int#) ->
        case GHC.Prim.remInt# x_ayH 2 of _ {
          __DEFAULT ->
            case x_ayH of wild_Xa {
              __DEFAULT -> List.result_go (GHC.Prim.+# wild_Xa 1);
              9223372036854775807 -> GHC.Types.[] @ GHC.Types.Int
            };
          0 ->
            case x_ayH of wild_Xa {
              __DEFAULT -> List.result_go (GHC.Prim.+# wild_Xa 1);
              9223372036854775807 -> GHC.Types.[] @ GHC.Types.Int
            }
        }
    

    A plain loop, running from 1 to maxBound :: Int, producing nothing on the way and [] at the end.
    It’s almost smart enough to plain return []. Note that there’s only one division by 2, GHC knows that if an Int is even, it can’t be odd, so that check has been eliminated, and in no branch a non-empty list is created (i.e., the unreachable branches have been eliminated by the compiler).

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

Sidebar

Related Questions

I have this part of script from my GAE application which uses webapp2, which
I want to code this part of a VB6 application in c#. How can
This part of the question provides background information and can be ignored I am
This part of my code works fine: #include <stdio.h> int main(){ //char somestring[3] =
This part of my code is used to award a grade to a student
This part of an app that I am working on, I have the following
Considering this part of a Java class, private List<Object> components = new ArrayList<Object>(); private
I added this part of the code in my onCreate() method and it crashes
Could you please clarify this part of Apple's documentation: Transitioning to ARC Release Notes
The end goal of this part of my project is to be able to

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.