[Haskell-cafe] code length in Haskell, a comparison
Richard O'Keefe
ok at cs.otago.ac.nz
Tue Nov 20 23:36:03 CET 2012
On 20/11/2012, at 4:55 PM, Gregory Guthrie wrote:
> There is some interesting data in the article at:
>
> Code Length Measured in 14 Languages
> http://blog.wolfram.com/2012/11/14/code-length-measured-in-14-languages/
>
> basically comparing program lengths in various languages, and some ensuing discussion of how this relates to language expressiveness, etc.
> (He does all of his analysis in Mathematica, which is the goal of the article.)
I'm not sure how interesting it actually is.
Let's study just one example: the digit sum problem.
"This task is to take a Natural Number in a given Base and return the sum of its digits."
We are not given any bounds on the size of the Natural Number
nor any bounds on the Range. Arguably, the code for almost all of the
programming languages shown is *wrong* due to making unwarranted
assumptions about integer size.
The Haskell example there is
digsum base = f 0 where
f a 0 = a
f a n = f (a+r) q where
(q,r) = n `divMod` base
main = print $ digsum 16 255 -- "FF": 15 + 15 = 30
Since some other examples assume the base is 2..16, so may we.
In that case,
import Data.Char
import Numeric
digsum b n = sum . map digitToInt $ showIntAtBase b intToDigit n ""
will do the job, and as an exercise in golfing myself,
import Numeric
digsum b n = sum . map fromEnum $ showIntAtBase b toEnum n ""
will also do it, and should work for larger bases.
In this particular test, Mathematica happens to score well
because it already has IntegerDigits[number{, base}?] in its
library.
So at least this exercise is much more about "what happens to
be available already in the library for this language" than
about anything intrinsic to the language.
There is worse. Some versions read test data, while some have
a small number of test cases built in, and they do not agree
about which test cases are provided.
Looking at some other problems, some tasks have versions that
use code that is published on the web but not part of the
language standard and not included (or counted!) at the Rosetta
site itself.
There are lots of things you can learn by looking at the
Rosetta site. I am not sure that the compactness of languages
is one of them.
More information about the Haskell-Cafe
mailing list