[Haskell-cafe] puzzling polymorphism behavior (7.0.3 windows)

Никитин Лев leon.v.nikitin at pravmail.ru
Thu Mar 15 18:30:41 CET 2012


Maybe everytime you use 'cell' you tell haskell to create NEW cell.

Try this:

push' i cell = modifyIORef cell (++ [i])

main = do
   cell <- newIORef []
   push' "x" cell {- push' 3 cell will be incorrect in this case -}
   push' "o" cell
   readIORef cell >>= return


Why the original code porduces [(),()]  but not [] I cannot undestand.

15.03.2012, 23:53, "gladstein at gladstein.com" <gladstein at gladstein.com>:
> Why does the following program compile and produce the results it does?
> It seems like 3 and "x" got interpreted as the same type, (). Thanks in
> advance for your help.
>
> import Data.IORef
> import System.IO.Unsafe
>
> cell = unsafePerformIO $ newIORef []
>
> push i = modifyIORef cell (++ [i])
>
> main = do
>  push 3
>  push "x"
>  readIORef cell >>= return
>
> *Main> :browse
> cell :: GHC.IORef.IORef [a]
> push :: a -> IO ()
> main :: IO [a]
>
> *Main> main
> [(),()]
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list