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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:59:29+00:00 2026-05-15T16:59:29+00:00

I have the following function defined: ex 1 x = 1 ex 0 x

  • 0

I have the following function defined:

ex 1 x = 1
ex 0 x = 0
ex b x = b ** x

Then, when I execute the following:

1 `ex` (sum [1..])

it tries to calculate the sum of the infinite sequence, instead of being lazy and returning 1. Why?


EDIT: Upon further investigation, I found that laziness happens if I define the ex function in a file, but not if I define it in GHCI:

$ ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> let ex 1 x = 1
Prelude> let ex b x = b ** x
Prelude> ex 1 (sum [1..])
<interactive>: out of memory (requested 1048576 bytes)

If I pull the ex definition into a file (in this case, test.hs):

$ ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> :load test.hs 
[1 of 1] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> ex 1 (sum [1..])
1.0

The new question, then, is why?

  • 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-15T16:59:29+00:00Added an answer on May 15, 2026 at 4:59 pm

    In GHCi, each let statement introduces a new definition of ex, instead of multiple pattern cases as you expect. So it hangs because, when you enter ex 1 (sum [1..]) afterwards, only the final ex b x = b ** x version exists.

    If you want to define a function with multiple pattern cases in GHCi, you’ll need to put it in a single let statement, like this:

    let ex 1 x = 1; ex 0 x = 0; ex b x = b ** x
    

    The same applies for anything else that would normally be written across multiple lines, such as do notation. For instance, a function like this:

    f x = do
        y <- get
        put (x + y)
        return y
    

    Would have to be written like this in GHCi:

    let f x = do { y <- get; put (x + y); return y }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function defined inside my linked list class. The declaration in
I have the following object defined function BusinessUnit(id, name, code) { this.id = ko.observable(id);
in header file I have defined the following function #ifndef OS_H #define OS_H #include
I have defined the following two functions test <- function(t) { return( (0.5*eta^2/theta)*(1-exp(-2*theta*t)) )
I have the following function defined: function _getUserMatches(url, mDist, ui) { var dataString =
I have created a class-managed GUI in which the following methods are defined function
I have following function in one Activity public void AppExit() { Editor edit =
In my MVC application in Controller i have following function to add and focus
have the following function on my collection : getFiltered: function (status, city) { return
I have the following function set up to sequentially fade in divs (with class

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.