[comp.lang.smalltalk] A smalltalk "list" operation

abbott@aerospace.aero.org (Russell J. Abbott) (09/08/89)

If anyone is interested I've discovered a cute way to implement a "list"
operation in smalltalk.  If this is old hat, excuse my mentioning it.

In Lisp, one can write, for example,

	>  (list (plus 1 2) 4)

and get

	(3 4)

That is, "list" takes an arbitrary number of arguments and returns a
list containing the evaluations of those arguments.  In smalltalk there
appears to be no equiivalent.  One could write something like:
	#( (1 + 2) 4)
but one would get an array containing two elements, the first of which
is an array containing the three elements: 1, +, and 2.  That is, there
is no evaluation.

The way around this is to define:

    (1) an instance creation method (which I call "<") that inserts
	its argument into the newly created object and

    (2) an "add:" method (which I call ",")

for Collection.  Then one can write:

	Set < (1 + 2), 4

This will yield: Set(3 4).  The "<" operator should be read similarly to
its use in the Unix shell as "redirect," i.e., create a set and put
these elements into it.  Of course, this structure may be nested:

	Set < 1, (Set < (2+3), 4), 5

which yields:  Set(1, Set(5, 4), 5)


While doing this, I found myself wishing that smalltalk had a parser
similar to prolog's in which one could define and redefine operators and
their precedences.  The smalltalk syntax is so simple, such a parser
should not be too much to ask.
-- 
-- Russ abbott@itro3.aero.org

johnson@p.cs.uiuc.edu (09/12/89)

Russ Abott's list operation is very cute.  One potential problem is
that "," is already defined in SequenciableCollection to concatenate
two collections.  It is real handy with strings.

abbott@aerospace.aero.org (Russell J. Abbott) (09/13/89)

In article <80500077@p.cs.uiuc.edu> johnson@p.cs.uiuc.edu writes:
>
>Russ Abott's list operation is very cute.  One potential problem is
>that "," is already defined in SequenciableCollection to concatenate
>two collections.  It is real handy with strings.

For just that reason I now use "/", e.g., Set / 1 / 2 / 3 / 4 ...  .
The "/" message is defined at both the class and the instance levels.
Of course one can pick one's own operator symbol.
-- 
-- Russ abbott@itro3.aero.org