
8 Dec
2003
8 Dec
'03
8:51 p.m.
Jeffrey A. Scofield wrote:
Say I have the following function, adapted from Pierce, Types and Programming languages:
f n () = (n, f (n + 1))
But a simple modification seems to cures the problem:
newtype W = W (Int, () -> W)
f n () = W (n, f (n + 1))
w2list (W (n,f)) = n:(w2list $ f ())
*Main> take 5 $ w2list (f 7 ()) [7,8,9,10,11] It works in Haskell98. Actually, at first I thought your problem is more complex. I thought it requires existential quantification to get the infinite type:
{-# OPTIONS -fglasgow-exts #-}
-- Emulating Peano integers
data W = forall a. (Show a) => W a
instance Show W where show (W a) = show a
f 0 = W ([]::[()]) f n = case f (n-1) of (W x) -> W [x]
*Main> f 5 [[[[[[]]]]]]