[Haskell-beginners] fix
Brandon S. Allbery KF8NH
allbery at ece.cmu.edu
Wed Oct 15 19:04:03 EDT 2008
On 2008 Oct 15, at 18:51, Matthew J. Williams wrote:
> fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5
> 120
>
> My interpretation:
> fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5
> ((\rec n -> if n == 0 then 1 else n * rec (n-1)) (fix (\rec n -> if
> n == 0 then 1 else n * rec (n-1)) )) 5
> . . .
>
> Yet, it does not quite explain how 'fix' does not result in
> infinite recursion.
Remember, Haskell is non-strict. When the computation reaches 0, the
"then" branch of the conditional is evaluated and the "else" is
unneeded and therefore ignored, so its re-invocation isn't seen.
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH
More information about the Beginners
mailing list