[Haskell-cafe] about Haskell code written to be "too smart"
Jonathan Cast
jonathanccast at fastmail.fm
Tue Mar 24 14:48:42 EDT 2009
On Tue, 2009-03-24 at 19:42 +0100, Manlio Perillo wrote:
> Tim Newsham ha scritto:
> >> These friends are very interested in Haskell, but it seems that the
> >> main reason why they don't start to seriously learning it, is that
> >> when they start reading some code, they feel the "Perl syndrome".
> >>
> >> That is, code written to be "too smart", and that end up being totally
> >> illegible by Haskell novice.
> >>
> >> I too have this feeling, from time to time.
> >>
> >> Since someone is starting to write the Haskell coding style, I really
> >> suggest him to take this "problem" into strong consideration.
> >
> > When you think about it, what you are saying is that Haskell programmers
> > shouldn't take advantage of the extra tools that Haskell provides.
>
> No, I'm not saying this.
>
> But, as an example, when you read a function like:
>
> buildPartitions xs ns = zipWith take ns . init $ scanl (flip drop) xs ns
>
> that can be rewritten (argument reversed) as:
>
> takeList :: [Int] -> [a] -> [[a]]
> takeList [] _ = []
> takeList _ [] = []
> takeList (n : ns) xs = head : takeList ns tail
> where (head, tail) = splitAt n xs
Huh? This is ugly and un-readable. Seriously.
> I think that there is a problem.
Damn straight. It should be:
> buildPartitions xs ns =
> zipWith take ns $ init $ scanl (flip drop) xs ns
Or, if you're really worried about blocks/line, you can increase the
line count a bit (I do this regularly):
> buildPartitions xs ns =
> zipWith take ns $ -- Select just the indicated prefix of
each element
> init $ -- Skip the last (empty) element
> scanl (flip drop) xs $ -- Cumulatively remove prefixes of
indicated length
> ns
> The buildPartition contains too many "blocks".
> And I have read code with even more "blocks" in one line.
>
> It may not be a problem for a "seasoned" Haskell programmer, but when
> you write some code, you should never forget that your code will be read
> by programmers that can not be at your same level.
Not if I can help it.
More seriously, beginner code belongs in the first two-three chapters of
Haskell programming textbooks, not anywhere else. It's like putting Fun
with Dick & Jane-speak in an adult novel.[1]
> I think that many Haskell programmers forget this detail, and IMHO this
> is wrong.
>
> > Haskell provides the ability to abstract code beyond what many other
> > programming systems allow. This abstraction gives you the ability to
> > express things much more tersely. This makes the code a lot harder to
> > read for people who are not familiar with the abstractions being used.
>
> The problem is that I have still problems at reading and understanding
> code that is too much terse...
> Because I have to assemble in my mind each block, and if there are too
> many blocks I have problems.
jcc
[1] Well, not that bad. Beginner-level code is useful for teaching the
basics of the language; Fun with Dick & Jane is child abuse.
More information about the Haskell-Cafe
mailing list