data type

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

data type

Alexander Chen
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]

I don't need the answer but more the intuition (a link to a youtube etc. is also possible) how to handle these types of situations. Because i keep trying to think in terms of key:values (Python,Julia) which doesn't seem the right paradigm to approach this.

thanks in advance!

best,


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: data type

Francesco Ariis
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?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners