lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Vyacheslav Egorov <mraleph <at>> writes:

> Functions can manipulate only values, while macroses can manipulate 
> "segments of computation" (expressions and statements) (e.g. one cannot 
> write elegant looking ifthenelse function, there will be a bunch of 
> lambdas)

I would argue that Smalltalk's style of if-then-else is elegant, mostly 
because the lambdas are elegant.  :)

  -- Lua
  if k > 7 then a:foo(k) else a:bar(k) end
  (k > 7)  ifTrue: [a foo:k]  ifFalse: [a bar:k]

The (k > 7) expression yields a Boolean object.  The ifTrue and ifFalse above 
are not language keywords, but simply a method of the Boolean class.  The 
method is passed lambdas, which are conveniently expressed with square 

Haskell provides even more elegant possibilities:

  -- Haskell
  iff (k > 7) (a foo k) (a bar k)

There actually is an if-then-else special form in Haskell, but it's easy to 
define as an ordinary function:

  iff True  x _ = x
  iff False _ y = y

This works because Haskell is lazy, so unused function arguments are never 

The point is that, in an expressive enough language, control structures can 
be defined with functions, not macros.  You just need a concise syntax for 
deferred evaluation (such as Smalltalk's brackets, or Haskell's defer-
everything policy).