Writing a counter function

Samuel E. Moelius III usmoeliu@mcs.drexel.edu
Sat, 29 Jun 2002 17:56:18 -0400


> 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].

Here's another not-exactly-what-you-wanted solution.  :)

If you don't mind changing your example to

	let (num1, next1) = out (counter 5)
	    (num2, next2) = out (next1 100)
	    (num3, next3) = out (next2 50) in
	    [num1,num2,num3]

then, you can do this:

	newtype Counter = MkCounter Int
	
	counter :: Int -> Counter
	counter n = MkCounter n
	
	out :: Counter -> (Int,Int -> Counter)
	out (MkCounter n) = (n,MkCounter . (n +))

Sam Moelius