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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:29:57+00:00 2026-05-31T20:29:57+00:00

GHC compiles it without a hitch, but it fails miserably at runtime: many_a x

  • 0

GHC compiles it without a hitch, but it fails miserably at runtime:

many_a x =
    let
        a = 2
    in
        let 
            a = 2*a
        in
            x*a

Intuitively, this shouldn’t work. But yet GHC accepted it.

  • 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-31T20:29:59+00:00Added an answer on May 31, 2026 at 8:29 pm

    Yes, that’s valid Haskell code. The thing is that the a in the second let...in expression is actually a new a; it’s perfectly permissible to shadow existing variables by defining a new variable with the same name. However, it doesn’t affect the value of the outer a. It’s generally considered bad style, however, due to the potential for confusion and mistakes; if you pass -Wall to GHC, it’ll issue a warning if you do this.

    It “fails” at runtime because you’ve defined a to be 2*a, which results in an infinite loop at runtime. This is due to laziness; basically, (*) evaluates both its arguments before multiplying them. Evaluating 2 works fine, of course, but evaluating a causes it to go through the same process all over again. The same thing that allows infinite lists (like ones = 1:ones) makes this code be an infinite loop.

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

Sidebar

Related Questions

One code work from runghc, but I can not compile same one with ghc
As I understand it, GHC (the Glorious Glasgow Haskell Compiler) compiles Haskell to Core,
I'm getting a warning from GHC when I compile: Warning: This binding for 'pats'
Here is the output from verbose mode. I know this used to work for
I have a huge haskell file which compiles and runs without any problem. I
This code does not compile in GHC 7.0.3: import System.IO main = do z
I'm using GHC 7.4.1 to try to compile a program that uses Repa. But
I have the following line of code which when compiled with GHC it goes
Will GHC perform tail-call optimization on the following function by default? The only weird
Can GHC or some lint tool tell me when I've provided a type signature

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.