Writing a counter function

A./C. Luis Pablo Michelena lmichele@multi.com.uy
Sun, 30 Jun 2002 05:26:34 -0300


This is a multi-part message in MIME format.

------=_NextPart_000_0024_01C21FF6.B345B0E0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I adjunt some code that i supose is what are you locking for.
Is based in a previous message (by jefu on 13 of june) idea from the
"Haskell acumulator" thread.
the "prueba" function is the translation of your example.
By the way, what's the purpose of this coding? (this is the type of
question: "ok, I have a hammer, now, for what kind of nail it is useful?")
Cheers,
            Luis Michelena
----- Original Message -----
From: Shlomi Fish <shlomif@vipe.technion.ac.il>
To: Hannah Schroeter <uk1o@rz.uni-karlsruhe.de>
Cc: <haskell-cafe@haskell.org>
Sent: Saturday, June 29, 2002 12:58 PM
Subject: Re: Writing a counter function


> On Sat, 29 Jun 2002, Hannah Schroeter wrote:
>
> > Hello!
> >
> > On Sat, Jun 29, 2002 at 06:23:27PM +0300, Shlomi Fish wrote:
> > > [...]
> >
> > > Actually, I'd like a more generalized counter. Something that would
return
> > > both the number and a handler to add another number, which in turn
would
> > > return the new sum and a new handler, etc.
> >
> > That's just what lazy lists are for. The "handler" thing is done
> > automatically thanks to lazy evaluation.
> >
> > I.e. countFrom n = n : countFrom (n + 1)
> > or just countFrom n = [n..]
> >
>
> No. But I want to generate an irregular series, which I determine the
> intervals between two consecutive numbers myself. E.g:
>
> let (num1, next1) = (counter 5)
>     (num2, next2) = (next1 100)
>     (num3, next3) = (next2 50) in
>     [num1,num2,num3]
>
> Will have the numbers [5, 105, 155].
>
> Regards,
>
> Shlomi Fish
>
> > Kind regards,
> >
> > Hannah.
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe@haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
>
>
>
> ----------------------------------------------------------------------
> 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.
>
>
>
>

------=_NextPart_000_0024_01C21FF6.B345B0E0
Content-Type: application/octet-stream;
	name="contador.hs"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="contador.hs"

data Contador = MkContador Integer (Integer -> Contador)

numero::Contador -> Integer
numero (MkContador a b)= a

funcion::Contador -> (Integer -> Contador)
funcion (MkContador a b)= b

cero:: Contador
cero=contado 0

contado::Integer -> Contador 
contado a= MkContador a otro
	where otro b=contado (a+b)

increcendo::Integer -> Contador
increcendo a= MkContador a otro
	where otro b=increcendo (a+1)

prueba::[Integer]
prueba=	let	MkContador num1 next1 = (contado 5)
		MkContador num2 next2 = (next1 100)
		MkContador num3 next3 = (next2 50) in
	[num1,num2,num3]
------=_NextPart_000_0024_01C21FF6.B345B0E0--