[erlang-questions] Orthogonality and Principle of least surprise Was: chained functions

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Mon Feb 6 17:24:43 CET 2012


On 2/1/12 9:05 AM, Jakob Praher wrote:
> Hi Erlangers,
>
> I think people should pay attention to issues faced by experienced newcomers in exercising Erlang programming. Matz from Ruby calls a ver important design principle, the principle of least surprise.
>
>
This is a good principle, but Ruby does not heed it:

http://stackoverflow.com/questions/372652/what-are-the-ruby-gotchas-a-newbie-should-be-warned-about

My personal favorite is this simple one:

def a
   7
   + 7
end

versus

def b
   7 +
   7
end

The a function returns 7 because the first 7 is produced as a side 
effect and the line '+ 7' evaluates to 7. The b function on the other 
hand produces an incomplete expression with 7 + and then adds 7 to it, 
returning 14 as you would expect. Note that in Erlang, where the grammar 
is more well-defined, there is no such distinction (note though that 
this is Ruby 1.8.7 - it may have been fixed in later ruby versions).

That is the reason for the prefixed 'fun' in Erlang: It makes the 
grammar unambiguous, like Richard O'Keefe mention - such that things 
like the above cannot occur.

-- 
Jesper Louis Andersen
   Erlang Solutions Ltd., Copenhagen, DK




More information about the erlang-questions mailing list