[Haskell-beginners] data type

Francesco Ariis fa-ml at ariis.it
Fri May 29 10:03:48 UTC 2020


Hello Alexander.

Il 29 maggio 2020 alle 11:55 Alexander Chen ha scritto:
> 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))
>     ]
> 
> My question:
> 
> I want to get the UTCTime.
> 
> So,  thisFunction :: [DatabaseItem] -> [UTCTime]

This is an excellent first step. A good candidate function is `map`

    λ> :t map
    map :: (a -> b) -> [a] -> [b]

and if we fill in the arguments we are sure of and leave the rest out,
ghc will — on reload/recompile — tell us what is missing. E.g:

    thisFunction :: [DatabaseItem] -> [UTCTime]
    thisFunction ds = map _ ds

will lead to

    • Found hole: _ :: DatabaseItem -> UTCTime
    • In the first argument of ‘map’, namely ‘_’

Can you write a function with `DatabaseItem -> UTCTime` signature?


More information about the Beginners mailing list