Ex 9.9 in Paul Hudak's book
Mon, 01 Apr 2002 00:10:02 +0200
At 16:23 31/03/2002 -0500, Antony Courtney wrote:
>Ludovic Kuty wrote:
>>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.
>Now let's define a generator to test out y:
>> factGen = \f -> \n -> if n==0 then 1 else n * f (n-1)
>> fact = y factGen
Thanks for the combinator.
That's really obscure :)
But it works:
remainder2 :: Integer -> Integer -> Integer
remainder2 = fix remainderGen
where remainderGen f a b = if a < b then a
else f (a - b) b