[Haskell-beginners] Improve my FFI code please...

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Jan 21 16:58:19 CET 2011


On Friday 21 January 2011 16:43:10, sean at objitsu.com wrote:
> Quoting Christian Maeder <Christian.Maeder at dfki.de>:
> > Am 21.01.2011 00:35, schrieb Sean Charles:
> >> wiiClose :: Ptr Word8 -> IO Bool
> >> wiiClose wptr = do
> >>     response <- c_wiimote_close wptr
> >>     case response of
> >>         0 -> return True
> >>         _ -> return False
> >
> > This can be rewritten to:
> >
> >   wiiClose = fmap (== 0) . c_wiimote_close
> >
> >>             case status of
> >>                 True -> putStrLn "OK"
> >>                 False -> putStrLn "FAIL"
> >
> > Matching Bool values use "case" is no good style:
> >
> >    putStrLn (if status then "OK" else "FAIL")
> >
> > Christian
>
> That's clever but you'd have to *know* haskell to *know* you could do
> that! Point-free (pointless!) is something I have yet to fully tackle,
> it looks like a great thing. Function composition is something else for
> my brain to remember exists in the language!
>
> IIUC: given a Ptr8 Word, call c_wiimote close then 'functor map' the
> 'section' (== 0) over the single entry in the list... what list? I am
> confused again because I cannot see a list, c_wiimote_close answers a
> pointer.

No list here, fmap 'lifts' a function to any Functor.
Here the Functor is IO.
Generally, for any Monad which is also an instance of Functor (any Monad 
should be),

fmap function monadicThingy

ought to be the same as

do value <- monadicThingy
   return (function value)

which is the sugared version of

monadicThingy >>= return . function

>
> I understand (== 0) rather than (0 ==) though, that's something!

Both should be the same function.

>
> Thanks for your time.
>
> :)




More information about the Beginners mailing list