Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

gopnik haskell

Name: noko-tan 2008-12-16 15:34

Hi guise
wai this code work not as planned
i expect "boo" but it prints Nothing

    import System.IO
    import Control.Monad
    import Data.IORef
    import qualified Data.HashTable as HT

    type MyTable = HT.HashTable String String

    newMyTable :: IO MyTable
    newMyTable = HT.new (==) HT.hashString

    data App = App {
        table :: IO MyTable,
        msg   :: IO (IORef String)
    }

    mkApp = App newMyTable (newIORef "")
    put app k v = liftM (\t-> HT.insert t k v) (table app)
    look app k = join (liftM (\t -> HT.lookup t k) (table app))

    main = do
        let app = mkApp
        put app "foo" "boo"
        boo <- look app "foo"
        print boo

Name: Anonymous 2008-12-16 15:41

>>1
join (liftM (\t -> HT.lookup t k) (table app))
table app is an IO MyTable, i.e. a constructor of hashtables. Your join/liftM combo runs the constructor and calls lookup on it, which returns Nothing, as is expected for new (and empty) hashtables.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List