[Haskell-beginners] Type unions

Hector Guilarte hectorg87 at gmail.com
Tue Dec 14 22:29:43 CET 2010


But did you already understand what's happening?

> data AorB = A | B

is pretty much like an enumeration the same as

> data DayOfTheWeek = Monday | Tuesday | Wednesday | ... | Sunday

it stores no values of any type, you could even have DayOfTheWeek as
a constructor in the same declaration of the data DayOfTheWeek, because they
are
in different scopes:

> data DayOfTheWeek = Monday | Tuesday | Wednesday | ... | Sunday
| DayOfTheWeek

and by no means, that A or B on the right hand side of your data AorB are
referencing the
data A nor the data B...

In the end, you could have:

data A = Aconstructor Int
data B = Bconstructor Int
data AorB = A A | B B

f :: Int -> AorB
f x
  | even x     = A (Aconstructor x)
  | otherwise = B (Bconstructor x)

I just added a Value to each constructor of the data AorB, which happens to
be
named the same as the value they store...

What I'm about to do is something I haven't tried, but I don't see why it
shouldn't
compile:

> data Try = Int Int

in data Try you have a constructor named Int, and it has a value of type
Int,

I'm trying to explain it the best I can, but I don't know if I managed to do
it
clearly, please let me know if there's something where I wasn't clear
enough.

Hector


On Tue, Dec 14, 2010 at 4:40 PM, Russ Abbott <russ.abbott at gmail.com> wrote:

> My typo started this most recent confusion. When I wrote
>
> data AorB = A | B
>
> compiles with error.
> That raises the question of what it really means!
>
>
> I meant to say
>
>  data AorB = A | B
>
> compiles *without *error.
> That raises the question of what it really means!
>
> *
> -- Russ *
>
>
> On Tue, Dec 14, 2010 at 1:04 PM, Tobias Brandt <tob.brandt at googlemail.com>wrote:
>
>> On 14 December 2010 22:02, Hector Guilarte <hectorg87 at gmail.com> wrote:
>> > Tobias you replied at the same time I was answering, you did explained
>> what
>> > is happening,
>> > however, you said something which isn't right. As I mentioned before,
>> "Type
>> > Constructors" and
>> > "Value Constructors" are in different scopes, and so, their names can be
>> > used again...
>> > if you don't believe me, give it a try with this example.
>>
>> I believe you. As I said, I was talking crap :-)
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20101214/3889c0ad/attachment.htm>


More information about the Beginners mailing list