I am working my way through the Haskell book and am on chapter 8. While doing the exercises I noticed something I didn't understand.
Why does this result in stack overflow
mc x | x>100 = x-10
| otherwise = mc $ mc x+11
but this doesn't
mc x | x>100 = x-10
| otherwise = mc $ mc (x+11)
I think it has something to do with x+11 not being evaluated in the first example but aren't expressions like that always evaluated
for example
Prelude> id 43+94
137
The first expression
mc $ mc x+11
is interpreted as
mc ((mc x) + 11)
since function application takes precedence over operators.
The second expression
mc $ mc (x+11)
is interpreted as:
mc (mc (x+11))
The first indeed will never get evaluated, since if you write:
mc x | x > 100 = x-10
| otherwise = mc ((mc x) + 11)
then you define mc x
in terms of mc x
. Unless that mc x
in that expression is not evaluated, you thus will call mc x
, when calculating mc x
, and thus it will keep making calls.