tags:

views:

123

answers:

1

I'm trying to create a 'Person' type where each person has a sex and a name.

data Sex = Sex Char deriving Show

male   = Sex 'M'
female = Sex 'F'

data Name   = Name [Char]      deriving Show

data Person = Person { 
    Sex    :: Sex,
    Name   :: Name
} deriving (Show)

When I try to load this in ghci I just get the unhelpful error parse error on input 'Sex'

What am I doing wrong here?

+9  A: 

The problem lies in your usage of upper case inside the record syntax. The code should look like:

data Person = Person { sex :: Sex, name :: Name }...

Trying that, the code seems to at least compile. Since "sex" and "name" are not types (while "Sex" and "Name" are), you cannot make the first letters upper case.

avpx
That's it exactly, thanks! I'm still learning the difference between type constructors, value constructors, etc. All very confusing and new to me. :)
friedo
I learned Haskell this summer, and it was a very rewarding experience. Hang in there!
avpx