[Haskell-cafe] Question on leazy evaluation

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Fri Mar 25 09:13:48 EST 2005


Still on the same subject, I solved one problem but don't really 
understand why it didn't work in the first place ! Although it seems to 
be a difference between "newtype" and "data" !

I have a newtype defined as :

data Fct s a = Fct (s -> [a])

and a function defined by :

plus :: Fct a b -> Fct a b -> Fct a b
plus (Fct f1) (Fct f2) = Fct ( \ a -> (f1 a) ++ (f2 a) )

For some reason, this function does not use leazy evaluation ! I can 
test it using :

test_fct :: Fct Int Int
test_fct = Fct( \ i -> [i] )

value = head $ apply (foldr plus test_fct $ repeat test_fct) 12

... trying to get "value" does not terminate !

But if I change the type declaration into :

newtype Fct s a = Fct (s -> [a])

... it works ! The "plus" function uses leazy evaluation and "value" can 
be computed.

Now, I didn't really understood the difference between newtype and data. 
Can anyone explain that behaviour ?

Thank you,

Pierre

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Haskell-Cafe mailing list