[Haskell-beginners] understanding type constructors and value constructors

Anwar Ludin anwar.ludin at gmail.com
Thu Sep 14 04:04:26 UTC 2017


Hello everyone,

I have just started studying Haskell and I am having a hard time
understanding type and value constructors.

So to create a new type, you write something like:

data FinancialInstrument = Financial  String Double
                                           deriving (Eq, Show)

and then you can write:

ibm = Financial "ibm" 150

OK all good. This initializes a FinancialInstrument. What I don't quite
grasp is what is the purpose of Financial (the data/value constructor)? And
from what I have read, you could have also written:

data FinancialInstrument = FinancialInstrument String Double
                                           deriving (Eq, Show)

To me the second expression is a lot closer to the typical OOP way of doing
things (where the type name and constructor(s) have the same name). Why
would someone prefer the first notation?

Once a value has been constructed, how can I access its fields?

Is there a way to create values using named parameters?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170914/ebcf899a/attachment.html>


More information about the Beginners mailing list