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 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.

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