[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-beginners
Subject: Re: [Haskell-beginners] filtering on a datatype
From: Bob Ippolito <bob () redivi ! com>
Date: 2020-06-02 16:45:33
Message-ID: CACwMPm_vKiofJp0VxxQayMajKcPc6uup9uqPvzf5NH8A=8CVaA () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
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 <alexander@chenjia.nl> 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
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
[Attachment #5 (text/html)]
<div><div dir="auto">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).</div></div><div dir="auto"><br></div><div dir="auto">mapMaybe is the sort \
of function you may to use for this purpose.</div><div dir="auto"><br></div><div \
dir="auto">Here are some type signatures as a hint for one way to implement \
it:</div><div dir="auto"><br></div><div dir="auto">dbDateTime :: DatabaseItem -> \
Maybe UTCTime</div><div dir="auto"><br></div><div dir="auto">onlyDateTimes :: \
[DatabaseItem] -> [UTCTime]</div><div dir="auto"><br></div><div dir="auto">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. </div><div dir="auto"><br></div><div \
dir="auto">-bob</div><div><br><div class="gmail_quote"><div dir="ltr" \
class="gmail_attr">On Tue, Jun 2, 2020 at 09:06 Alexander Chen <<a \
href="mailto:alexander@chenjia.nl">alexander@chenjia.nl</a>> \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex"><div \
style="font-family:Arial;font-size:13px"><div>Hi,</div><div><br></div><div>given \
this:</div><div><div \
style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas,"Courier \
New",monospace;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span \
style="color:#569cd6">import</span> <span \
style="color:#4ec9b0">Data.Time</span></div><br><div><span \
style="color:#569cd6">data</span> <span style="color:#569cd6">DatabaseItem</span> = \
DbString <span style="color:#569cd6">String</span></div><div> \
| DbNumber <span style="color:#569cd6">Integer</span></div><div> \
| DbDate <span style="color:#569cd6">UTCTime</span></div><div> \
<span style="color:#569cd6">deriving</span> (<span \
style="color:#569cd6">Eq</span>, <span style="color:#569cd6">Ord</span>, <span \
style="color:#569cd6">Show</span>)</div><br><div><span \
style="color:#dcdcaa">theDatabase</span> :: [<span \
style="color:#569cd6">DatabaseItem</span>]</div><div>theDatabase =</div><div> \
[ DbDate (UTCTime</div><div> (fromGregorian <span \
style="color:#b5cea8">1911</span> <span style="color:#b5cea8">5</span> <span \
style="color:#b5cea8">1</span>)</div><div> (secondsToDiffTime <span \
style="color:#b5cea8">34250</span>))</div><div> , DbNumber <span \
style="color:#b5cea8">9001</span></div><div> , DbString <span \
style="color:#ce9178">"Hello, world!"</span></div><div> , DbDate \
(UTCTime</div><div> (fromGregorian <span \
style="color:#b5cea8">1921</span> <span style="color:#b5cea8">5</span> <span \
style="color:#b5cea8">1</span>)</div><div> \
(secondsToDiffTime <span style="color:#b5cea8">34123</span>))</div><div> \
]</div></div><div><div \
style="font-family:Arial;font-size:13px"><br></div></div></div><div \
style="font-family:Arial;font-size:13px"><br></div><div \
style="font-family:Arial;font-size:13px">question from textbook is : write a function \
that filters for DbDate values and returns a list of the UTCTime values inside \
them.<br></div><div style="font-family:Arial;font-size:13px"><br></div><div \
style="font-family:Arial;font-size:13px"><br></div><div \
style="font-family:Arial;font-size:13px">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.</div><div \
style="font-family:Arial;font-size:13px"><br></div><div \
style="font-family:Arial;font-size:13px">thanks in advance.</div><div \
style="font-family:Arial;font-size:13px"><br></div><div \
style="font-family:Arial;font-size:13px">best,</div><div \
style="font-family:Arial;font-size:13px"><br></div><div \
style="font-family:Arial;font-size:13px"><br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" \
target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br> \
</blockquote></div></div>
[Attachment #6 (text/plain)]
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic