[Haskell-cafe] loop problem

Alexander Berntsen alexander at plaimi.net
Sat May 10 17:49:41 UTC 2014


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 10/05/14 19:45, Stuart A. Kurtz wrote:
> Nice, although I'm a bit perplexed as to why it typechecks without 
> hints, whereas the following (which avoids the boxing/unboxing of 
> pairs) does not:
> 
> mapM_ (\n -> printf "%d %d" n (n^2)) [1..100]
This type checks and works fine in GHCi.

? :t mapM_ (\n -> printf "%d %d" n (n^2)) [1..100]
mapM_ (\n -> printf "%d %d" n (n^2)) [1..100]
  :: (Monad m, PrintfType (m b)) => m (

? mapM_ (\n -> printf "%d %d" n (n^2)) [1..100]
1 12 43 94 165 256 367 498 649 8110 10011 12112 14413 16914 19615 22516
25617 28918 32419 36120 40021 44122 48423 52924 57625 62526 67627 72928
78429 84130 90031 96132 102433 108934 115635 122536 129637 136938 144439
152140 160041 168142 176443 184944 193645 202546 211647 220948 230449
240150 250051 260152 270453 280954 291655 302556 313657 324958 336459
348160 360061 372162 384463 396964 409665 422566 435667 448968 462469
476170 490071 504172 518473 532974 547675 562576 577677 592978 608479
624180 640081 656182 672483 688984 705685 722586 739687 756988 774489
792190 810091 828192 846493 864994 883695 902596 921697 940998 960499
9801100 10000

But if I chuck it in a source file and attempt to runhaskell or compile
it, this fails with a very nasty error. Oh, what fun! To make it work ni
a source file, you need a type signature in there somewhere. E.g.:

main = mapM_ (\n -> printf "%d %d" (n :: Int) (n^2)) [1..100]

This works fine. This is indeed interesting! Someone care to
additionally make it enlightening as well?
- -- 
Alexander
alexander at plaimi.net
https://secure.plaimi.net/~alexander
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iF4EAREIAAYFAlNuZrUACgkQRtClrXBQc7U7+wD/QKhDvJawLyzziks+QZ2ypVQz
qSglFRu+TCsNXQJqrVsA/jmS8x3xit3lypPX3y3Lmctl9tgudSLsPxccba5qcYXd
=CiiY
-----END PGP SIGNATURE-----


More information about the Haskell-Cafe mailing list