Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record syntax for a constructor

Tags:

haskell

I wonder why does this work

data Person = PersonContructor {
  firstName :: String,
  lastName :: String,
  age :: Int
} deriving (Show)

main = putStrLn $ show $ map (PersonContructor "firstName1" "lastName1") [666, 999]

and this doesn't

data Person = PersonContructor {
  firstName :: String,
  lastName :: String,
  age :: Int
} deriving (Show)

main = putStrLn $ show $ map (PersonContructor {firstName="firstName1", lastName="lastName1"}) [666, 999]

and how do I fix it?

like image 929
Alan Coromano Avatar asked Aug 09 '13 08:08

Alan Coromano


1 Answers

While Constructors act like curried functions in general, so you can partially apply them as in your first example, the record syntax constructing wants to construct a complete record with no fields left out.

If you want to name the fields nevertheless, you can use a lambda:

map (\age -> PersonContructor {firstName="firstName1", lastName="lastName1", age=age}) [666, 999]
like image 85
firefrorefiddle Avatar answered Oct 31 '22 00:10

firefrorefiddle