beginner's questions - fix f
Bob Koutsky
bck@webhome.cz
Tue, 24 Jul 2001 09:49:33 +0200
Hello,
After watching members of this list discussing some for me really
incomprehensible details of Haskell standard, I feel sort of silly for
asking something probably very basic, but I'd be very grateful for any answer:
Some time ago, I have decided to educate myself in functional programming,
and I choose Haskell as a language of choice. I purchased "Haskell school
of expression" and began to study it. Everything went well, I understood
(at least intuitively) everything, but then, suddenly, I hit a wall:
------------------------------------------------------------
Exercise 9.9:
remainder a b = if a < b then a
else remainder (a-b) b
fix f = f (fix f)
Rewrite remainder using fix so that it is not recursive.
------------------------------------------------------------
Function fix left me completely puzzled. With help of hugs I found out that
its type is "( a -> a ) -> a", but I have absolutely no idea how it could
be used to do anything useful. It seems to me that argument to f on the
right side of fix's definition must always evaluate to the same value, no
matter how deep the recursion is, so there is no point to use it. I guess I
am missing something very obvious, but what is it? Can somebody provide me
with an example how to use fix for something just a bit useful, if possible
to rewrite remainder?
thank you,
Bob Koutsky