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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:25:18+00:00 2026-05-27T20:25:18+00:00

As someone relatively new to Haskell and functional programming, and coming mainly from a

  • 0

As someone relatively new to Haskell and functional programming, and coming mainly from a Python background, I’d like to know why the following function results in a stack overflow in Haskell, even if I use a very low number like 4, or 5 as the input variable, whereas the exact same function in Python can process an integer of 20 and up without overflowing. Why is this so?

countStairs <0 = 0
countStairs 0 = 1
countStairs n = countStairs(n-1) + countStairs(n-2) + countStairs(n-3)

I’ve read other responses on Haskell and stack overflows that address code optimization and solving specific overflowing code, whereas I’m interested in understanding the specifc reason for the difference in how the two languages handle recursion here, or more generally why the Haskell code results in a stack overflow.

EDIT: I didn’t include the full python code because this is my first question in Stack Overflow and I’m struggling to figure out how to get my Python to format properly (nice welcome from some of you, btw). Here it is, poor formatting and all, but the Python as written does work properly with the integer 20, whereas my undoubtedly poor Haskell has not. I’ve edited the Haskell code to show the corresponding code I originally omitted. I thought I was including the relevant recursive part, but obviously I was wrong to omit the base case. Still, as written, my Haskell stack overflows and my Python doesn’t, and I’m still interested in learning why. Even though I don’t come from a programming background, I really like learning Haskell and was just trying to learn some more. Thanks to those who tried to address the question in spite of my incomplete question.

def countStairs(n):
    if n < 0:
        return 0
    elif n == 0:
        return 1
    else:
        return countStairs(n-1) + countStairs(n-2) + countStairs(n-3)

myint = int(raw_input("Please enter an integer: "))
print countStairs(myint)
  • 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-27T20:25:18+00:00Added an answer on May 27, 2026 at 8:25 pm

    Another way to add a terminating condition is to use a guard (this also addresses the <= 2 condition Corbin mentioned:

    countStairs n | n > 0 = countStairs(n-1) + countStairs(n-2) + countStairs(n-3)
                  | otherwise = 1
    

    Update

    Your updated Haskell example doesn’t work because you misunderstand a how pattern matching works. You were expecting it to work like a guard (seeing as you tried to provide a boolean expression < 0 in a pattern match), however, that version of your function never matches (when you call the countStairs function). Consider this example:

    countStairs < 0 = "Matched '< 0'"
    countStairs   0 = "Matched '0'"
    countStairs   n = "Matched n"
    
    main = do
        putStrLn $ countStairs (-1)  -- outputs: "Matched n"
        putStrLn $ countStairs 0     -- outputs: "Matched 0"
        putStrLn $ countStairs 20    -- outputs: "Matched n"
    

    The interesting thing here is that your function actually compiles. To find out why, load the above code into ghci and type :browse. This will give you a list of the functions you’ve defined in this module. You should see something like this:

    (Main.<) :: Num a => t -> a -> [Char]
    countStairs :: Num a => a -> [Char]
    main :: IO ()
    

    You’ve got countStairs and main which both make sense. But you’ve also got this function called Main.<. What is this? You’ve redefined the < function in this module! In case you’re not familiar, you can define infix functions (like +, <, >, etc.) like so:

    infix <
    a < b = True
    
    -- also defined as
    (<) a b = True
    

    Normally, you need that infix FUNCTION_NAME to indicate your function is infix. But.. prelude already defines < as an infix function, therefore, you didn’t need to, and instead just gave your own definition of <.

    Now, lets rearrange our countStairs < 0 = "Matched '< 0'" like we did with a < b, and you get this:

    (<) countStairs 0 = "Matched '< 0'"
    

    In this function, countStairs is actually the first argument to your < function.

    Here’s one more example to drive home the point. Try running 1 < 0 in ghci (with your module still loaded). Here’s what you’ll get:

    *Main> 1 < 0
    
    <interactive>:1:3:
        Ambiguous occurrence `<'
        It could refer to either `Main.<', defined at foo.hs:3:13
                              or `Prelude.<', imported from Prelude
    

    Normally, you’d get False, but in this case, ghci doesn’t know if it should use your function (since < is just a regular function, not a special syntax) or the built-in (Prelude) version of <.

    Long story short… use guards (or case, or if) for boolean tests, not pattern matches.

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

Sidebar

Related Questions

I am relatively new to Python and was hoping someone could explain the following
Being relatively new to functional programming, I expend lots of energy wondering is this
This may be a silly question, but as someone relatively new to PHP, I'm
I'm relatively new to hibernate and was wondering if someone could help me out.
Relatively new to python. I recently posted a question in regards to validating that
What are some good resources for learning OpenGL 4.1 aimed at someone relatively new
I'm relatively new to C. I've come across a form of function syntax I've
I'm relatively new to programming and a recent project I've started to work on
I'm relatively new to MySQL stored procs, so I was hoping someone could help
I'm relatively new to Cocoa/ObjC. Could someone please help me change my code 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.