Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/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. filtering on a datatype (Alexander Chen)
2. Re: filtering on a datatype (Bob Ippolito)
----------------------------------------------------------------------
Message: 1
Date: Tue, 2 Jun 2020 18:05:51 +0200 (CEST)
From: Alexander Chen <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] filtering on a datatype
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hi,
given this:
import Data.Time
data DatabaseItem = DbString String
| DbNumber Integer
| DbDate UTCTime
deriving (Eq, Ord, Show)
theDatabase :: [DatabaseItem]
theDatabase =
[ DbDate (UTCTime
(fromGregorian 1911 5 1)
(secondsToDiffTime 34250))
, DbNumber 9001
, DbString "Hello, world!"
, DbDate (UTCTime
(fromGregorian 1921 5 1)
(secondsToDiffTime 34123))
]
question from textbook is : write a function that filters for DbDate values and
returns a list of the UTCTime values inside them.
my question could you give me an example of a working function, I don't get how
i use the filter function on a data type in a list. Hence i am kinda stuck.
thanks in advance.
best,
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20200602/62953207/attachment-0001.html>
------------------------------
Message: 2
Date: Tue, 2 Jun 2020 09:45:33 -0700
From: Bob Ippolito <[email protected]>
To: Alexander Chen <[email protected]>, The Haskell-Beginners
Mailing List - Discussion of primarily beginner-level topics related
to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] filtering on a datatype
Message-ID:
<CACwMPm_vKiofJp0VxxQayMajKcPc6uup9uqPvzf5NH8A=8c...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I can see why you’re a bit confused here, the question is poorly written.
You can not use the Prelude filter function to do this, but you do need to
write a function that filters and maps over the data to do this
transformation (onlyDateTimes below is an example type signature for such a
function).
mapMaybe is the sort of function you may to use for this purpose.
Here are some type signatures as a hint for one way to implement it:
dbDateTime :: DatabaseItem -> Maybe UTCTime
onlyDateTimes :: [DatabaseItem] -> [UTCTime]
There are of course other ways to implement onlyDateTimes such as directly
using pattern matching and recursion, or using foldr, concatMap, etc. I
would go with whichever method you’ve learned from the textbook so far.
-bob
On Tue, Jun 2, 2020 at 09:06 Alexander Chen <[email protected]> wrote:
> Hi,
>
> given this:
> import Data.Time
>
> data DatabaseItem = DbString String
> | DbNumber Integer
> | DbDate UTCTime
> deriving (Eq, Ord, Show)
>
> theDatabase :: [DatabaseItem]
> theDatabase =
> [ DbDate (UTCTime
> (fromGregorian 1911 5 1)
> (secondsToDiffTime 34250))
> , DbNumber 9001
> , DbString "Hello, world!"
> , DbDate (UTCTime
> (fromGregorian 1921 5 1)
> (secondsToDiffTime 34123))
> ]
>
>
> question from textbook is : write a function that filters for DbDate
> values and returns a list of the UTCTime values inside them.
>
>
> my question could you give me an example of a working function, I don't
> get how i use the filter function on a data type in a list. Hence i am
> kinda stuck.
>
> thanks in advance.
>
> best,
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20200602/277bcce6/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 144, Issue 1
*****************************************