[Haskell-beginners] Improve my FFI code please...
Henk-Jan van Tuyl
hjgtuyl at chello.nl
Fri Jan 21 10:30:02 CET 2011
On Fri, 21 Jan 2011 10:09:23 +0100, <sean at objitsu.com> wrote:
> Edward, Daniel, thanks.
>
>> On Friday 21 January 2011 00:35:02, Sean Charles wrote:
>> wiiOpen :: String -> IO (Maybe (Ptr Word8))
>> wiiOpen wid = do
>> ? ?handle <- withCString wid c_wiimote_open
>> ? ?case handle of
>> ? ? ?nullPtr -> return Nothing
>> ? ? ?handle -> return (Just handle)
>> Unless nullPtr is a macro that gets replaced with a literal by the
>> preprocessor, that won't do what you want. "nullPtr" is a generic name
>> and
>> matches everything, so you'll always take the first branch (with -Wall,
>> ghc
>> should warn about overlapping patterns in that case).
>
> Daniel, you have confused me. In my RWH book there is a test from the
> PCRE example of nullPtr == x, so I just did my code using a case
> instead, I am sure it's correct!?!?!? If I run my program with the
> Wiimote off, it prints FAIL and if I press the red sync. button it then
> prints out OK and the memory address of the allocated structure so it
> surely must be workng as I expected?
>
> Anybody?
>
The Haskell 2010 Language Report[0] says that case expressions work with
patterns; the nullPtr is regarded as a pattern that matches anything.
It can be demonstrated by the following:
Program Case.lhs:
> c = 1
> f = case 0 of
> c -> print 0
> 1 -> print 1
GHCi session:
Prelude> :load "Case.lhs"
[1 of 1] Compiling Main ( Case.lhs, interpreted )
Case.lhs:8:4:
Warning: Pattern match(es) are overlapped
In a case alternative: 1 -> ...
Ok, modules loaded: Main.
*Main> f
0
If the c in the case expression was a constant, f would have printed 0
Regards,
Henk-Jan van Tuyl
[0]
http://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-460003.13
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--
More information about the Beginners
mailing list