Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
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... (Christian Maeder)
2. Re: Improve my FFI code please... ([email protected])
3. Re: Improve my FFI code please... (Daniel Fischer)
4. Question about List Type Constraint and Null (aditya siram)
5. Re: Question about List Type Constraint and Null
(Magnus Therning)
----------------------------------------------------------------------
Message: 1
Date: Fri, 21 Jan 2011 13:46:36 +0100
From: Christian Maeder <[email protected]>
Subject: Re: [Haskell-beginners] Improve my FFI code please...
To: Sean Charles <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
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
------------------------------
Message: 2
Date: Fri, 21 Jan 2011 15:43:10 +0000
From: [email protected]
Subject: Re: [Haskell-beginners] Improve my FFI code please...
To: Christian Maeder <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; DelSp="Yes";
format="flowed"
Quoting Christian Maeder <[email protected]>:
> 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.
I understand (== 0) rather than (0 ==) though, that's something!
Thanks for your time.
:)
------------------------------
Message: 3
Date: Fri, 21 Jan 2011 16:58:19 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Improve my FFI code please...
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On Friday 21 January 2011 16:43:10, [email protected] wrote:
> Quoting Christian Maeder <[email protected]>:
> > 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.
>
> :)
------------------------------
Message: 4
Date: Sat, 22 Jan 2011 00:03:07 -0600
From: aditya siram <[email protected]>
Subject: [Haskell-beginners] Question about List Type Constraint and
Null
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi all,
The following function gives me an "Ambiguous type variable `a' in the
constraint: `Read a' arising from a use of `res'" error:
test :: Read a => String -> Maybe [(a,String)]
test s = if null res then
Nothing
else
Just $ fst $ head res
where
res = reads s
The reason as 'jmcarthur' so patiently explained on IRC is that 'res'
is used twice and the type constraint 'a' is different for each use,
hence the ambiguity. I get that.
But I have a further question why should 'null ...' care about the
type of its list argument? Isn't it polymorphic? So it shouldn't make
a difference that the 'a' inside res is ambiguous because we know for
sure that it always returns a list of some kind.
Thanks,
-deech
------------------------------
Message: 5
Date: Sat, 22 Jan 2011 08:07:05 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
and Null
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On 22/01/11 06:03, aditya siram wrote:
> Hi all,
> The following function gives me an "Ambiguous type variable `a' in the
> constraint: `Read a' arising from a use of `res'" error:
> test :: Read a => String -> Maybe [(a,String)]
> test s = if null res then
> Nothing
> else
> Just $ fst $ head res
> where
> res = reads s
This code doesn't give me that error, in fact it gives me no error at all.
/M
--
Magnus Therning OpenPGP: 0xAB4DFBA4
email: [email protected] jabber: [email protected]
twitter: magthe http://therning.org/magnus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110122/c4b24d43/attachment-0001.pgp>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 31, Issue 21
*****************************************