[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Aug 23 10:09:00 1999 
Subject:Re: a little list problem 
From:David Young 
Volume-ID:990823.02 

On Mon, 23 Aug 1999, Terry Dartnall wrote:

> This little procedure is in TEACH LISTANSWERS.  It's meant to take a list such
> as [a b c] and return [[a][b][c]], listifying every item in the original list.
> Instead, it returns [[a] [b] c], and doesn't listify the last item in the
> input list.  Can someone tell me why?  (The program in LISTANSWERS has an
> output local - same difference.)
> 
> define listify(list);
> vars item;
> [^^(for item in list do [^item] endfor)]
> enddefine;
> 
> I'm running v 14.5.

The ^^ construct splices a list into another list. Crudely speaking, it
strips a set of list brackets off its argument and puts the contents
into the enclosing list. So [ ^^( [a] ) ] is just [a].

The procedure given above, with [a b c] as input, in effect has as an
intermediate result

    [ ^^( [a], [b], [c] ) ]

at the point where the for loop has run but the ^^ hasn't yet acted.
Since ^^ expects a single list as argument, it strips the brackets off
the [c] but leaves the other two lists alone, so when the outer list is
built you get the result you report. You can check this explanation: the
line above can be executed as I've typed it.

The teach file is thus incorrect, and the correct procedure is

    define listify1(list);
        vars item;
        [^(for item in list do [^item] endfor)]
    enddefine;

since the single ^ builds the list required without stripping any of the
brackets inserted in the loop.

Surprising that no-one's commented before in the 15 years the teach file
has been around! It must always have produced the error. I'll fix it in
the Sussex masters, though I don't know whether that will propagate now.

David