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: help with IO guards (Sylvain Henry)
2. Re: Defining an instance: Syntax that works exactly
sometimes (Jeffrey Brown)
3. Re: Defining an instance: Syntax that works exactly
sometimes (Brandon Allbery)
4. Show and showList (Animesh Saxena)
----------------------------------------------------------------------
Message: 1
Date: Fri, 23 Jan 2015 19:50:40 +0100
From: Sylvain Henry <[email protected]>
To: [email protected], The Haskell-Beginners Mailing List -
Discussion of primarily beginner-level topics related to Haskell
<[email protected]>
Subject: Re: [Haskell-beginners] help with IO guards
Message-ID:
<capmptcv4gqlbqe6zmodvxgq_w6fxu9faajmmcywa+tccfly...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi,
Using LambdaCase extension, you can write something quite elegant:
{-# LANGUAGE LambdaCase #-}
f :: Int -> IO String
f x = getDBRecord x >>= \case
dbOutput
| null dbOutput -> return "no db record"
| otherwise -> return "we got some db records"
Or better:
f :: Int -> IO String
f x = getDBRecord x >>= \case
[] -> return "no db record"
_ -> return "we got some db records"
But you can also use another function for the pure part:
recordMsg :: [a] -> String
recordMsg [] = "no db record"
recordMsg _ = "we got some db records"
f :: Int -> IO String
f = fmap recordMsg . getDBRecord
Regards,
Sylvain
2015-01-15 12:51 GMT+01:00 Miro Karpis <[email protected]>:
> Hi,
>
> please is there a way to have guards with 'where' that communicates with
> IO? Or is there some other more elegant way? I can do this with classic
> if/else,...but I just find it nicer with guards.
>
>
> I have something like this (just an example):
>
>
> f :: Int -> IO String
> f x
> | null dbOutput = return "no db record"
> | otherwise = return "we got some db records"
> where dbOutput = getDBRecord x
>
>
> getDBRecord :: Int -> IO [Int]
> getDBRecord recordId = do
> putStrLn $ "checking dbRecord" ++ show recordId
> --getting data from DB
> return [1,2]
>
>
> problem is that db dbOutput is IO and the guard check does not like it:
>
> Couldn't match expected type ?[a0]? with actual type ?IO [Int]?
> In the first argument of ?null?, namely ?dbOutput?
> In the expression: null dbOutput
>
>
>
> Cheers,
> Miro
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150123/2243ce7f/attachment-0001.html>
------------------------------
Message: 2
Date: Fri, 23 Jan 2015 11:39:25 -0800
From: Jeffrey Brown <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
works exactly sometimes
Message-ID:
<CAEc4Ma0KKhp1y5wf=qq2bk5bmwzfyzffepylxn5k9vnjljc...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Should I always look first in the 98 Report, and only if I don't find
something there then look in the User's Guide? Should I always look through
both, lest something in the 98 Report be obsolete? Is nothing in it
obsolete?
On Thu, Jan 22, 2015 at 7:40 PM, Brandon Allbery <[email protected]>
wrote:
> On Thu, Jan 22, 2015 at 10:34 PM, Jeffrey Brown <[email protected]>
> wrote:
>
>> I have not found the "deriving" keyword's specification anywhere.
>
>
> The correct place to look for this and other things, including instances,
> is the Haskell Report.
>
>
> https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-780004.3.3
> 4.3.3. Derived Instances
>
>
> https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-770004.3.2
> 4.3.2. Instance Declarations
>
> --
> brandon s allbery kf8nh sine nomine
> associates
> [email protected]
> [email protected]
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150123/5effd873/attachment-0001.html>
------------------------------
Message: 3
Date: Fri, 23 Jan 2015 15:01:53 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
works exactly sometimes
Message-ID:
<CAKFCL4V-40f2wu=rr6tecpcpux_nkwky-rxzmvr2ceckb6m...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Fri, Jan 23, 2015 at 2:39 PM, Jeffrey Brown <[email protected]>
wrote:
> Should I always look first in the 98 Report, and only if I don't find
> something there then look in the User's Guide? Should I always look through
> both, lest something in the 98 Report be obsolete? Is nothing in it
> obsolete?
You really want the 2010 report, not the 98 one.
https://www.haskell.org/onlinereport/haskell2010/
You may first check the extensions part of the GHC manual:
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghc-language-features.html
Usually extensions will be called out with a pragma, though, so if there
are not {-# LANGUAGE #-} sections at the top then you probably don't need
to check the GHC manual. If there are, look over the sections of the GHC
manual relating to those LANGUAGE pragmas.
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150123/fdd769e8/attachment-0001.html>
------------------------------
Message: 4
Date: Sat, 24 Jan 2015 01:21:20 +0000 (GMT)
From: Animesh Saxena <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Show and showList
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
I am trying to understand the concept of overlapping instances. In the book
"Real World Haskell" there is an example of showList which avoids overlapping
instances.?
As per my understanding showList is taking the same problem that method "show"
would have for overlapping instances. Here's the snippet which explains how it
works...
"The Show class defines both a show method, which renders one value, and a
showList method, which renders a list of values. The default implementation of
showList renders a list using square brackets and commas.
The instance of Show for [a] is implemented using showList. The instance of
Show for Char provides a special implementation of showList that uses double
quotes and escapes non-ASCII-printable characters.
As a result, if someone applies show to a [Char] value, the implementation of
showList will be chosen, and it will correctly render the string using quotes.
At least sometimes, then, we can avoid the need for the OverlappingInstances
extension with a little bit of lateral thinking."?
GHC Code
class Show a where
? showsPrec :: Int -> a -> ShowS
? show :: a -> String
? showList :: [a] -> ShowS
*Main> show [1,2,3]
"[1,2,3]"
*Main> show ['a','b','c']
"\"abc\""
*Main>
It's the same problem now transfered to showList which will be declared
differently for a list of int's or a list of chars, but still it outputs them
differently as can be seen from the above code.?How it is able to differentiate
without throwing up the overlap error?
-A
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150124/44016630/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 79, Issue 26
*****************************************