Evaluation Question
Simon Marlow
simonmar@microsoft.com
Mon, 13 Jan 2003 11:28:51 -0000
> For each call, I believe. An exception might be if the call to nco is
> inlined into the calling function, but this is unlikely as nco is
> recursive.
>=20
> So, you're probably better off with:
>=20
> > nco wn =3D nco'
> > where wn' =3D cis wn
> > nco' =3D 1 : map (wn'*) nco'
>=20
> In which case it will only be evaluated once.
The original version should also evaluate the expression 'cis wn' only
once:
> nco :: RealFloat a =3D> a -> [ Complex a ]
> nco wn =3D 1 : map ((*) (cis wn)) (nco wn)
You can verify this using an appropriate semantics for lazy evaluation.
However, another way to think about it is to consider the expression
(*) (cis wn)
as
(let x =3D cis wn in (*) x)
which when first evaluated will create a suspension for x in the heap,
and return
=09
\y -> (*) y x
as its value. So x only gets allocated/evaluated once.
Cheers,
Simon