Sunday, November 28, 2010

Revenge of Smalltalk

(Joke inside)

In Paul Graham's essay Revenge of the Nerds (Read it, good ideas there):

Appendix: Power

As an illustration of what I mean about the relative power of programming languages, consider the following problem. We want to write a function that generates accumulators-- a function that takes a number n, and returns a function that takes another number i and returns n incremented by i.

(That's incremented by, not plus. An accumulator has to accumulate.)

In Common Lisp this would be


(defun foo (n)
  (lambda (i) (incf n i)))


In Smalltalk the code is slightly longer than in Lisp:


foo: n                              
  |s|                      
  s := n.                          
  ^[:i| s := s+i. ]

because although in general lexical variables work, you can't do an assignment to a parameter, so you have to create a new variable s.



Well, the Lisp code has 34 significant characters while the Smalltalk code has only 25 significant characters (we can remove the last dot). So the Smalltalk code is 26% shorter than the Lisp code :)


  • Perl example: 32 characters
  • Javascript example: 46 characters
  • Python example: 54 characters
Yeah, Smalltalk is really powerful ;P


Update:
Looking at Parser code we can easily remove the temp. variable limitation and write:
foo: n               
  ^[:i| n := n+i]

So
  • One power of Smalltalk is to be able to change the system.
  • 17 significant characters: Smalltalk 100% more powerful than Lisp :D
  • (still a joke - but ....)


Why this design decision ? Some explanation from Nicolas and Adrian.

8 comments:

  1. Hey! Ruby is not far from Small.

    def foo(n)
    lambda{ |i| n += i}
    end

    ReplyDelete
  2. 29 characters ... 16% higher :) But thanks for the snippet

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I trust you didn't miss the memo on "syntax" in lisp: http://symbo1ics.com/blog/?p=275

    ReplyDelete
  5. haskell way: foo = \x i -> x + i

    ReplyDelete
  6. arggg better than Smalltalk !! How can it be possible ????? :) BTW I'm a Haskell newbie but really impressed by the language.
    Thank you !

    ReplyDelete
  7. Hahaha! Thanks for the morning laugh :-D

    ReplyDelete
  8. Interesting how haskel example will look without value accumulation?
    In smalltalk I mean [:n | [:i| n+i]]

    ReplyDelete