
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,

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
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
participants (2)
-
Alexander Chen
-
Bob Ippolito