Data Constructors and Readability
I think I’ve put my finger on one reason I’m finding Haskell hard to read. Consider this algebraic data type definition from Real World Haskell:
data Doc = Empty | Char Char | Text String | Line | Concat Doc Doc | Union Doc Doc deriving (Show, Eq)
There’s no distinction between the value constructor and the components of the type. This is especially critical when the constructor and the components have the same name as in the Char
constructor above. It also doesn’t help that they have the same naming convention (mixed camel case, initial uppercase letter).
A Java class with multiple factory methods or multiple constructors would be much more verbose, but much more readable. Humans need redundancy, even if it’s logically superfluous.
I suspect a language that retained Haskell’s semantics, but completely replaced the syntax with something more usable, legible, and familiar would have a much greater chance of success.
February 5th, 2009 at 1:03 PM
Well, you have to remember that what you call “legible” is what other people call “illegible”.
Readers, it’s a myth that ERH writes Java. What he actually writes is this:
February 5th, 2009 at 8:18 PM
Can you post the Java equivalent of the Doc data type defined above?
February 6th, 2009 at 10:01 AM
Java doesn’t have algebraic data types and pattern matching as such, so there these would probably be subclasses. E.g.
and so forth. This isn’t ideal because this isn’t how you’d solve this problem in Java.
However if one were to imagine a true Haskell equivalent that used Java-like syntax but Haskell semantics you might see something like this:
That could mean exactly the same thing as the actual Haskell; but I for one would find it much easier to read. Yes, it takes a little longer to type, but ease of reading trumps ease of typing.
Of course we could play with the exact keywords. Maybe
adt
ordata
instead ofdatatype
; maybenew
instead ofconstructor
, but you get the idea.February 6th, 2009 at 8:13 PM
Try Mercury: like Haskell, but more parentheses :)
February 6th, 2009 at 8:14 PM
…and my nicely formatted <pre> has been mangled, sigh.
February 6th, 2009 at 8:39 PM
I fixed your comment. I think you have to register to be allowed to use HTML, but I turned off registration a few months ago because no one did, and a security hole had been found in it. Let me turn it back on and see what happens.
February 6th, 2009 at 8:44 PM
OK, Let’s see if this works:
This is some
preformatted
text
so much depends on
a red Haskell
furiously thinking
green thoughts
February 6th, 2009 at 8:45 PM
Nope, that didn’t work.
February 6th, 2009 at 8:51 PM
OK. The problem isn’t registration. It’s that pre is specifically disallowed (more accurately, not specifically allowed). let me see if I can fix that.
February 6th, 2009 at 8:53 PM
Let’s see if this works now:
February 6th, 2009 at 8:53 PM
Aha! It worked. Everyone should be able to use pre here now. I’ll to make the same change on the Cafes.
Thanks for the nudge.
February 6th, 2009 at 8:54 PM
I should probably set the stylesheet not to make pre text bold though…
September 29th, 2014 at 6:15 AM
ERH,
Very interesting post, loved it! I too tried to learn Haskell in the past, and totally understand your post.