
02.09.2010 16:49, Eoin C. Bairéad пишет:
Example 2
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
How does it know to stop ?
To stop what? It's not "doing" anything, it's just an equation. So "fac" is the "least" function which satisfies this equation - meaning that it's value would be (_|_) (which is a special value called "bottom") whenever possible. When you ask for "fac 3", for example, it's not possible for it to be (_|_), because it has to be 3*fac(2), according to this equation; and "fac 2" isn't bottom either, because it has to be 2*fac(1); and fac(1) has to be 1*fac(0), which is also not bottom, because the equation states explicitly that fac 0 = 1.
and why does fac 2.5 hang?
Because there isn't anything to prevent "fac 2.5" from being equal to (_|_); and when you ask for value that happens to be (_|_), your program can either crash or hang forever, it's the only special thing about bottom value.