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.

Name: Anonymous 2008-12-16 15:54

>>1
Haskell records ultimately destructive1
                                         
References: [1] http://bloggablea.wordpress.com/2007/04/24/haskell-records-considered-grungy/

Name: noko-tan 2008-12-16 16:00

>>2
I try to pack several HashTables in single data
how can I do this?

Name: Anonymous 2008-12-16 17:14

>>2
thank u sweet anon
me understand

    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 :: MyTable,
        msg   :: IO (IORef String)
    }

    mkApp = do
        ht <- newMyTable
        return (App ht (newIORef ""))
       
    --tab (IO (App t _)) = t
   
    appInsert app k v = HT.insert (table app) k v
    appLookup app k = HT.lookup (table app) k
   
    main = do
        app <- mkApp
        appInsert app "foo" "boo"
        boo <- appLookup app "foo"
        print boo

Name: Anonymous 2008-12-16 17:56

>>3
I don't really get the problem with the prefix record accessing syntax. Why not do something simple like this?

infixl 8 .>>   -- flip ($)
infixl 8 .>=   -- >>=
infixl 8 .$>   -- fmap
rec .>> acc = acc rec
rec .>= acc = acc =<< rec -- different predence
rec .$> acc = acc `fmap` rec

data Bird = Bird { name :: String, wings :: Integer, parent :: Maybe Bird }
hubert = Bird { name = "Hubert", wings = 3, parent = Nothing }
fred = Bird { name = "Fred", wings = 2, parent = Just hubert }
instance Show Bird where
    show bird = "{ A bird called " ++ bird .>> name .>> show ++ " with " ++ bird .>> wings .>> show ++ " wings" ++
        maybe "" (\n -> " and a parent called " ++ n ++ "") (bird .>> parent .$> name .$> show) ++ " }"


It's not the prettiest thing ever, but it's better than chaining accessors backwards.

ghci> fred
{ A bird called "Fred" with 2 wings and a parent called "Hubert" }
ghci> hubert
{ A bird called "Hubert" with 3 wings }
ghci> fred .>> parent .>> fmap wings
Just 3
ghci> fred .>> parent .$> wings
Just 3
ghci> fred .>> parent .>= parent .>= parent .$> wings
Nothing


Obviously record updates don't work.

Name: Anonymous 2008-12-17 9:22

>>6
I have to admit, that's fairly slick. I still don't think I'd use records though.

Name: Anonymous 2010-11-23 8:41

test

Name: Anonymous 2010-11-23 8:41

test

Name: Anonymous 2010-11-23 9:12

test

Name: Anonymous 2010-11-23 9:17

test2

Name: Anonymous 2011-02-03 1:55

Name: Anonymous 2011-02-03 3:30


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