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)
exact same
python program outputs forcountStairs 20
– Ingo