Q: Forcing repeated evaluation
Martin Norbäck
d95mback@dtek.chalmers.se
12 Sep 2002 11:43:42 +0200
--=-8sekqQQTXTB9rg89YmVv
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
tor 2002-09-12 klockan 11.27 skrev Jan Kybic:
> Hello,
> I have another question regarding the optimisation of Haskell cod=
e:
> I have a relatively inexpensive function generating a long list, imagine
> something like (I simplified a lot):
>=20
> l =3D [ i*i*i | i <- [0..n] ] -- for very large n
>=20
> This long list is consumed several times in the program:
>=20
> x1 =3D f1 l
> x2 =3D f2 x1 l
> x3 =3D f3 x2 l
>=20
> I found that the list l is calculated just once and that the
> computational time is dominated by the allocations and garbage
> collection. I want to try to force l to be generated on-the-fly
> every time it is needed, to see if it improves performance.
> What is a good way to do it? Would something like
>=20
> unsafePerformIO $ return l
>=20
> do the job? Isn'it there any flag for the compiler (ghc) to suggest
> this optimisation? Thank you for your feedback.
The easiest way is to make it a function
l _ =3D [ i*i*i | i <- [0..n] ] -- for very large n
x1 =3D f1 (l ())
x2 =3D f2 x1 (l ())
x3 =3D f3 x2 (l ())
() can be any value really.
Regards,
Martin
--=20
Martin Norb=E4ck d95mback@dtek.chalmers.se =20
Kapplandsgatan 40 +46 (0)708 26 33 60 =20
S-414 78 G=D6TEBORG http://www.dtek.chalmers.se/~d95mback/
SWEDEN OpenPGP ID: 3FA8580B
--=-8sekqQQTXTB9rg89YmVv
Content-Type: application/pgp-signature; name=signature.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: För information se http://www.gnupg.org/
iD8DBQA9gGHOkXyAGj+oWAsRAlLTAJ9jMa4u0wvBsiwcOG/ofPtIdRZVBACdFxYp
loa2VfE2PHfgpO2Z7dDW/mA=
=W0Bm
-----END PGP SIGNATURE-----
--=-8sekqQQTXTB9rg89YmVv--