Useful list output function
Shawn P. Garbett
Shawn@Garbett.org
Wed, 30 Jul 2003 14:51:44 -0500
=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I've got a little function which I derived from Bird's book "Intro to=20
=46unctional Programming using Haskell".
Basically I had several instances of lists that needed outputing with all=20
kinds of text decoration. This function was used repeatedly throughout the=
=20
code, by removing all the cut and paste instances, this was the common=20
function:
output :: (a -> IO b) -> [a] -> IO ()
output f =3D (foldr (>>) (return ())).(map f)
So given a function that transforms a value of type 'a' into an output of '=
IO'=20
and a list of 'a', all IO is performed across the list.=20
Example usage:
output f [1,2,3] where f a =3D putStr ("List contains "++(show a)++"\n"))
=2D --
List contains 1
List contains 2
List contains 3
Probably not very exciting for the old hats at Haskell, but pretty handy to=
=20
have around at my level of expertise.
Shawn
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iEYEARECAAYFAj8oIdAACgkQDtpPjAQxZ6AC0ACaA8boOZc8Hjmp9rTAyZ00lPLX
QwkAniPsBPHUvBfUcsZlU6TvF1pzkTox
=3D3aEG
=2D----END PGP SIGNATURE-----