Writing a counter function

that jefu guy jefu.jefu@verizon.net
29 Jun 2002 10:36:49 -0700


On Sat, 2002-06-29 at 15:26, Mark Carroll wrote:
> On Sat, 29 Jun 2002, Samuel E. Moelius III wrote:
> (snip)
> > Here's another not-exactly-what-you-wanted solution.  :)
> (snip)
> 
> Do any of the experimental extensions to Haskell allow a what-he-wanted
> solution? I couldn't arrange one in H98 without something having an
> infinitely-recursive type signature. I'm sure it would have been easy in
> Lisp, and he already gave a Perl equivalent, so I'm wondering if it could
> be at all sane for Haskell to allow such stuff and if Haskell is somehow
> keeping us on the straight and narrow by disallowing the exact counter
> that was originally requested.
> 
> The beauty of his request was that it was so simple and seemed to make
> sense; I went ahead and tried to fulfill it before realising I couldn't
> do it either.

I could not manage to do this with a simple always-increment-by-one
function, but the problem of adding a number n each time was a quite
a bit easier - though it still took me a while to escape the infinite
recursive type , it seems that you need to indirect through another 
datatype (here FP).  

you can't print z or z', but the show defined will allow you to print
out a FooPair

-----

data FooPair =  FP Integer (Integer -> FooPair) 

instance Show FooPair  where
   show (FP i f) = "FooPair " ++ (show i) ++ "...fun..."

incg :: Integer -> Integer -> FooPair 
incg n = \i ->  let j = n+i in  (FP j  (incg j))

val (FP i _) = i 
fun (FP _ f) = f 

x  = incg 7  -- the original function 
y  = x 3     -- increment the current value by 3 and return the FP pair
zf = fun y   -- get the new function
zv = val y   -- and the value in the pair 
z' = z 99    -- now get the next value function pair 

-----

--
jeff putnam -- jefu.jefu@verizon.net -- http://home1.get.net/res0tm0p