[Haskell-beginners] filtering on a datatype
bob at redivi.com
Tue Jun 2 16:45:33 UTC 2020
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
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.
On Tue, Jun 2, 2020 at 09:06 Alexander Chen <alexander at chenjia.nl> wrote:
> 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.
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners