Ex 9.9 in Paul Hudak's book

Ludovic Kuty kuty@advalvas.be
Sun, 31 Mar 2002 22:41:45 +0200


The exercise is short.

First, he defines a "fix" function:
	fix f = f (fix f)
Which has the type (a -> a) -> a

Then the function "remainder":
	remainder :: Integer -> Integer -> Integer
	remainder a b = if a < b then a else remainder (a - b) b

The function fix, has far as i understand the matter, has no base case.
So if someone wants to evaluate it, it will indefinitely apply the function f
to itself, leading the hugs interpreter to its end.

That's ok. But next he asks the reader to rewrite the function remainder
using fix and i cannot find out how.

I tried something like:
	remainder2 a b = fix (\x -> x - b)
but there is no base case too.

As soon as the lambda expression wants to evaluate its argument, the interpreter
crashes.

Can someone help me to solve the problem ?

TIA

Ludovic Kuty