what is normally done in smalltalk:
Dwight Hughes
dwighth at ipa.net
Sun Sep 5 01:18:25 UTC 1999
Mayer Goldberg wrote:
>
> Dear Squeakers:
>
> Here's a newbie question for you. :) You're writing a LISP interpreter in smalltalk (just
> to show someone how easy it is :) ). You're coding the evaluation of an if-then-else
> expression, which in LISP looks like this:
>
> (if test do-if-true do-if-false)
>
> You can either do:
>
> (exprTest value: anEnvironment)
> ifTrue: [^ exprDoIfTrue value: anEnvironment]
> ifFalse: [^ exprDoIfFalse value: anEnvironment].
>
> or
>
> ^ (((exprTest value: anEnvironment)
> ifTrue: [exprDoIfTrue]
> ifFalse: [exprDoIfFalse])
> value: anEnvironment).
>
> The first is more readable, the second is more concise, generates less code and
> shares the value: message passing to the doIfTrue, doIfFalse clauses.
>
> Question 1: What is considered more smalltalk-like? (my guess is the first)
Yes. Simplicity and straightforward readability is normally considered
rather more important than saving a couple of bytecodes for Smalltalkers
(and the performance of the first example is a tiny bit better for the
true branch).
> Question 2: Is the second form used at all? (my guess: ???)
Occasionally, but not commonly. BTW, the outer set of parens is not
really necessary since ^ returns the value of the whole expression it
preceeds.
If you are interested in seeing some other examples of Lisp interpreters
in Smalltalk, check out
ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.1/lisp/ (not
written for Squeak, but it should be fairly easy to understand) or the
Lisp goodie at
http://www.sra.co.jp/people/nishis/smalltalk/Squeak/goodies/ (a bit more
complex).
-- Dwight
More information about the Squeak-dev
mailing list
|