Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Most of Haskell syntax has beauty of purity. But the record syntax looks ugly. It's uncomfortable. It feels some kind of mixture with C. It requires comma and braces. Haskell has tab,line based separation. So it looks too verbose than originally it requires. Why is it designed in that way?

share|improve this question
3  
To me all of Haskell feels weird. What can I do? –  Job Feb 16 '11 at 3:48
7  
You aren't alone. Quite a few people complain about (the current incarnation of) records. –  delnan Feb 16 '11 at 14:51
    
Eonil: If you really hate record syntax, you can disable it with the {-# NoTraditionalRecordSyntax #-} GHC extension. –  Daniel Díaz Carrete Jun 13 at 13:43
add comment

1 Answer

up vote 8 down vote accepted

While I wasn't on the design commitee, I reckon that record syntax was shaped to be consistent with list syntax. Module export syntax uses commas, too, the only real place where layout is used is top-level declarations, where clauses and do-notation.

On top of that, given

data Foo = Foo {bar :: Int, baz :: Int}

writing

fnord x = x { bar = 4 }

without the braces would clash with the rest of the syntax, and using layout, like

fnord x = x
    bar = 4

would make the syntax quite brittle. Add a "where" in the wrong place and your code means something completely different.

If you're not happy with the state of records in Haskell (and you wouldn't be alone with that), I recommend you have a look at fclabels or even elaborate typeclass hackery like HList or grapefruit-records (the latter two not being for the faint of heart, but also insanely powerful)

fclabels would allow you to write (yay pointfree)

fnord = setL bar 4

as well as

getBar = getL bar

on top of it's actual raison d'etre, which is composing labels:

 data Person = Person { _place  :: Place, ... }
 data Place = Place { _city :: String, ... }

 moveToAmsterdam :: Person -> Person
 moveToAmsterdam = setL (city . place) "Amsterdam"
share|improve this answer
    
Hmm. Does setL mean mutable?? Without IO? IT's hard to understand... –  Eonil Feb 16 '11 at 11:19
2  
Not at all, you're going to get another (sharing) copy of the record with a single field changed, just like with record syntax. No mutability or break of referential transparency involved. –  barsoap Feb 16 '11 at 11:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.