[Haskell-beginners] Strange behavior of program

arjanen.loic at gmail.com arjanen.loic at gmail.com
Sun Mar 8 12:01:44 UTC 2015


Hello,


Given that you specifically indicated in your code that you wanted Int’s instance of Read and that the result of the product is way over 2 ^ 32 (or 2 ^ 64 for that matter), I’d guess the difference comes from the fact that the product of a is done with type Int (that is, modulo 32 or 64 depending on your computer’s CPU architecture) and the product with the litteral list is done with type Integer because that the type Haskell defaults to in this case.






ARJANEN Loïc Jean David
http://blog.luigiscorner.com
--- 
“Computer science is no more about computers than astronomy is about telescopes, biology is about microscopes, or chemistry is about beakers and test tubes. Science is not about tools. It is about how we use them, and what we find out when we do.”
Michael R. Fellows and Ian Parberry





De : m00nlight
Envoyé : ‎dimanche‎ ‎8‎ ‎mars‎ ‎2015 ‎21‎:‎59
À : The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell





    
Hi Haskellers,

I encounter an strange behavior of haskell program recently.  The following is my program

```haskell
main = do
    _ <- getLine
    arr1 <- getLine
    _ <- getLine
   arr2 <- getLine
   let a = map (read :: String -> Int) (words arr1)
        b = map (read :: String -> Int) (words arr2)

  putStrLn $ show $ (foldl (*) 1 a)
  putStrLn $ show $ a == [1,2,4,8,32,64,128,256,512,1024,2048,4906,8192]
  putStrLn $ show $ (foldl (*) 1 [1,2,4,8,32,64,128,256,512,1024,2048,4906,8192])
```

With the input test file as following:

```test.in
13
1 2 4 8 32 64 128 256 512 1024 2048 4906 8192
9
1 3 9 27 81 243 729 2187 6561
```

The output is as:
```output
0
True
185343439719667835347140608
```

In fact, from the program, we know that a is equal to list  [1,2,4,8,32,64,128,256,512,1024,2048,4906,8192] ,
but the product of a and the literal list is different.

Can anyone tell me why?

Thanks       














--m00nlight
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150308/5937dc8d/attachment.html>


More information about the Beginners mailing list