[Haskell-beginners] fix
Justin Bailey
jgbailey at gmail.com
Wed Oct 15 19:02:50 EDT 2008
n decreases on each step of the recursion, which will allow it to
terminate. You need to expand AND substitute arguments:
fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5
> fix (\rec 5 -> if 5 == 0 then 1 else n * rec (5 -1))
> fix (\rec 5 -> if 5 == 0 then 1 else n * (fix (\rec 4 -> if 4 == 0 then 1 else 4 * rec (3-1))))
And so on.
On Wed, Oct 15, 2008 at 3:51 PM, Matthew J. Williams
<matthewjwilliams1 at googlemail.com> wrote:
> hello listers, a few days ago A fellow lister sent me the following link:
>
> http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion
>
> The 'fix' function is interesting to say the least. There is one
> example that I've had difficulty expanding:
>
> 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.
>
> Sincerely
> Matthew J. Williams
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
More information about the Beginners
mailing list