Writing a counter function

Shlomi Fish shlomif@vipe.technion.ac.il
Sat, 29 Jun 2002 17:30:36 +0300 (IDT)


I'm trying to write a counter function that would return a tuple whose
first element is the current value and whose second element is a new
counter. The following line:

counter n = (n,(counter (n+1)))

Generates the following error on Hugs and a similar one with ghci:

ERROR "counter.hs":6 - Type error in function binding
*** Term           : counter
*** Type           : a -> (a,b)
*** Does not match : a -> b
*** Because        : unification would give infinite type

Is there any way to do it? I tried using data, type and newtype and none
of them worked.

Regards,

	Shlomi Fish

----------------------------------------------------------------------
Shlomi Fish        shlomif@vipe.technion.ac.il
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       shlomif@iglu.org.il

He who re-invents the wheel, understands much better how a wheel works.