Hacker Public Radio

HPR2758: Haskell - Data types and database actions


Listen Later

Intro
I have been doing series about web programming in Haskell and realized that I might have skipped over some very basic details. Better later than never, I’ll go over some of them briefly (data types and database actions). Hopefully things will make more sense after this (like with my friend, whose last programming course was programming 101 and they said afterwards that now all that 3d and game programming is suddenly making sense).
Data types
Data here has nothing to do with databases (yet). This is how you can declare your own data types in Haskell. They’re declared with keyword data followed with type name, equals sign and one or more value constructors. Type name and value constructors have to start with uppercase letter.
Simplest type is following:
data Simple = One
This declares a type called Simple that has single possible value: One.
More interesting type is shown below. Colour has three possible values: Red, Green and Blue.
data Colour =
Red
| Green
| Blue
It’s possible to have parameters in value constructor. Following is Payment type that could be used to indicate how payment was done. In case of Cash amount is stored. In case of IOU free text is recorded.
data Payment =
Cash Double
| IOU Text
Fictional usage of the Payment is shown below. Function paymentExplanation takes a Payment as parameter and returns Text describing the payment. In case of cash payment, brief explanation of how much was paid is returned. In case of IOU slip the function returns explanation stored in IOU value.
paymentExplanation :: Payment -> Text part is type declaration. It states that paymentExplanation takes argument of type Payment and returns result as Text.
paymentExplanation :: Payment -> Text
paymentExplanation payment =
case payment of
Cash amount ->
"Cash payment of " <> (show amount) <> " euros"
IOU explanation ->
explanation
Parameters don’t have to be hard coded in the type definition. Parametrized types allows creating more general code. Maybe is very useful data type that is often used for data that might or might not be present. It can have two values: Nothing indicating that there isn’t value and Just a indicating that value is present.
data Maybe a =
Nothing
| Just a
a is type parameter that is filled in when declaring type. Below is a function that takes Maybe Payment as a parameter and if value of payment parameter is Just returns explanation of it (reusing the function we declared earlier). In case of Nothing "No payment to handle" is returned.
invoice :: Maybe Payment -> Text
invoice payment =
case payment of
Just x ->
paymentExplanation x
Nothing ->
"No payment to handle"
Alternatively one can omit case expression as shown below and write different value constructors directly as parameters. In both cases, compiler will check that programmer has covered all cases and emit a warning if that’s not the case.
invoice :: Maybe Payment -> Text
invoice (Just payment) =
paymentExplanation payment
invoice Nothing =
"No payment to handle"
Having several parameters gets soon unwieldy, so lets introduce reco
...more
View all episodesView all episodes
Download on the App Store

Hacker Public RadioBy Hacker Public Radio

  • 4.2
  • 4.2
  • 4.2
  • 4.2
  • 4.2

4.2

34 ratings


More shows like Hacker Public Radio

View all
The Infinite Monkey Cage by BBC Radio 4

The Infinite Monkey Cage

1,952 Listeners

Click Here by Recorded Future News

Click Here

418 Listeners

Hacker And The Fed by Chris Tarbell & Hector Monsegur

Hacker And The Fed

168 Listeners