Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Improve my FFI code please... (s...@objitsu.com) 2. Re: Improve my FFI code please... (Henk-Jan van Tuyl) 3. Re: Improve my FFI code please... (Edward Z. Yang) 4. Re: Improve my FFI code please... (David Virebayre) 5. Re: Improve my FFI code please... (Henk-Jan van Tuyl) ---------------------------------------------------------------------- Message: 1 Date: Fri, 21 Jan 2011 09:09:23 +0000 From: s...@objitsu.com Subject: Re: [Haskell-beginners] Improve my FFI code please... To: beginners@haskell.org Message-ID: <20110121090923.11211yyfwlw93...@abe.enixns.com> Content-Type: text/plain; charset=ISO-8859-1; DelSp="Yes"; format="flowed" 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? Edward, I am going to apply your comments over the weekend and see if I can truly understand them. I get the rough idea. Thanks guys. Sean. ------------------------------ Message: 2 Date: Fri, 21 Jan 2011 10:30:02 +0100 From: "Henk-Jan van Tuyl" <hjgt...@chello.nl> Subject: Re: [Haskell-beginners] Improve my FFI code please... To: beginners@haskell.org, s...@objitsu.com Message-ID: <op.vpnjcch9pz0...@zen5.arnhem.chello.nl> Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes On Fri, 21 Jan 2011 10:09:23 +0100, <s...@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 -- ------------------------------ Message: 3 Date: Fri, 21 Jan 2011 04:36:30 -0500 From: "Edward Z. Yang" <ezy...@mit.edu> Subject: Re: [Haskell-beginners] Improve my FFI code please... To: sean <s...@objitsu.com> Cc: beginners <beginners@haskell.org> Message-ID: <1295602517-sup-3867@ezyang> Content-Type: text/plain; charset=UTF-8 Excerpts from sean's message of Fri Jan 21 04:09:23 -0500 2011: > 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? A more direct answer is that you need to use a guard in this case: case x of x | x == nullPtr -> ... Or maybe in this case, using a regular conditional would be better. You can pattern match against constructors (things that are capitalized); variable names (lower-cased) will just assign new variables. Cheers, Edward ------------------------------ Message: 4 Date: Fri, 21 Jan 2011 10:38:30 +0100 From: David Virebayre <dav.vire+hask...@gmail.com> Subject: Re: [Haskell-beginners] Improve my FFI code please... To: s...@objitsu.com Cc: beginners@haskell.org Message-ID: <aanlktinv6xeysvskitnbuumgzgggnhvxsept-2ie6...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 2011/1/21 <s...@objitsu.com>: > 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 It's not correct, pattern matching and equality testing is not the same thing. case handle of nullPtr -> return Nothing handle -> return (Just handle) here nullPtr and handle are new variables you introduce for pattern matching. naming one of them nullPtr is confusing because it's not trying to compare handle to nullPtr. it's basically saying: 1) if I can define nullPtr = handle, then return nothing 2) if I can define handle = handle, then return (Just handle) You see that in 1) you can always define nullPtr = handle, so the first case will always succeed. also in 2) it's a bit silly to pattern match a variable with a variable having the same name. if handle == nullPtr then return Nothing else return (Just handle) this is not the same thing, because here you test handle against nullPtr for equality, which implies nullPtr is already defined. David. ------------------------------ Message: 5 Date: Fri, 21 Jan 2011 11:15:22 +0100 From: "Henk-Jan van Tuyl" <hjgt...@chello.nl> Subject: Re: [Haskell-beginners] Improve my FFI code please... To: beginners@haskell.org, s...@objitsu.com, "Henk-Jan van Tuyl" <hjgt...@chello.nl> Message-ID: <op.vpnlfwqopz0...@zen5.arnhem.chello.nl> Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes On Fri, 21 Jan 2011 10:30:02 +0100, Henk-Jan van Tuyl <hjgt...@chello.nl> wrote: > > 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 That should have been: > 0 -> print 1 to be a proper example, but the result is identical. Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html -- ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 31, Issue 20 *****************************************