There is a stack exchange dedicated to code golfing,
http://codegolf.stackexchange.com/ . During free moments at work, I find
posting J solutions is a good way to relieve stress.

Today I came across a competition to reimplement square root in as few
characters as possible [1]. Most languages copped out and simply restated
sqrt as ^&0.5 to minimize their golf score.  (I did this too, to allow
apples-to-apples comparisons).

But the C++ entry caught my eye:  exp(log(x)/2)) .  This is an interesting
approach, and gives us an opportunity to highlight some of the advantages
of J's notaton. Leaving aside the question of syntax, even expressing the
4 primitives in that expression (exp, log, x, 2) would cost most languages
8 characters.  But not in J.  In J, we do NOT need:

   - To name variables (so no "x")
   - To name the constant 2 (in this particular case)
   - To delimit functions from arguments using parentheses
   - To explicitly instruct the intreprter to undo the log by typing "exp". 

This last freebie is perhaps the most satisfying.  We just tell the
interpreter that we want to operate "log space", and let J figure out the
exp for itself.

   -:&.^.

Six characters.  Less than it takes to write "log" and "exp" separated by a
single space!

-Dan

[1] 
http://codegolf.stackexchange.com/questions/73/reimplementing-square-root/15856

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to