understanding type constructors and value constructors

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

understanding type constructors and value constructors

Anwar Ludin
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! 

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

Re: understanding type constructors and value constructors

Alex Belanger
I'm on my phone which makes replying painful, but consider:

data Weekday = Monday | Tuesday | Wednesday | Thursday | Friday

data Shape = Circle Int | Rectangle Int Int Int Int | Triangle Int Int

data Either a b = Left a | Right b

Cheers,
Alex

On Sep 14, 2017 12:05 AM, "Anwar Ludin" <[hidden email]> wrote:
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! 

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

Re: understanding type constructors and value constructors

Imants Cekusins
This chapter details it:

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

More questions are welcome, of course.


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

Re: understanding type constructors and value constructors

Anwar Ludin
In reply to this post by Alex Belanger
@Alex,

OK that starts to make sense. Many thanks. Can we say that a data constructor is a way of expression polymorphic types? I don't know if that even makes sense in the Haskell context?

Thanks

Sent from Nylas Mail, the best free email app for work

On Sep 14 2017, at 6:56 am, Alex Belanger <[hidden email]> wrote:
I'm on my phone which makes replying painful, but consider:

data Weekday = Monday | Tuesday | Wednesday | Thursday | Friday

data Shape = Circle Int | Rectangle Int Int Int Int | Triangle Int Int

data Either a b = Left a | Right b

Cheers,
Alex

On Sep 14, 2017 12:05 AM, "Anwar Ludin" <[hidden email]> wrote:
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! 

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners