a little list problem
Author Message
a little list problem

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.

Thanks

Terry
==============================================
Terry Dartnall
School of Computing and Information Technology
Griffith University Nathan Brisbane
Queensland 4111 Australia
Phone:  61-7-3875 5020
Fax:    61-7-3875-5051
==============================================

Fri, 08 Feb 2002 03:00:00 GMT
a little list problem

Quote:

> 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

Fri, 08 Feb 2002 03:00:00 GMT
a little list problem
David,
Thanks for responding to Terry's message, with a perfect
diagnosis and explanation.

I have no idea how I allowed this buggy version of listify to get into

Quote:
> > define listify(list);
> > vars item;
> > [^^(for item in list do [^item] endfor)]
> > enddefine;

I have learnt from bitter experience that I need to test all
demonstration programs even the ones that are *obviously* correct !!.

Quote:
> 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.

Yes, though nowadays I would replace "vars" with "lvars" in that sort of
context.

And, in case anyone doesn't find it obvious, this

[^(for item in list do [^item] endfor)]

is equivalent to this

[% for item in list do [^item] endfor %]

which is probably better style.

Quote:
> 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 suspect we have a great deal of old teaching material that is not
really used.

Quote:
> I'll fix it in
> the Sussex masters, though I don't know whether that will propagate now.

I have installed a fixed version in
ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/bugfixes/teach/

with a note in
ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/bugfixes/BUGREPORTS

I shall eventually work out a scheme for updating all the masters and
the gzipped tar files here, but right now I am rather busy with other
things.

It looks as if the bham version will become the "main" version for a
while, though I don't know what will happen in the long run.

Aaron

Sat, 09 Feb 2002 03:00:00 GMT
a little list problem
Aaron

Quote:
> [% for item in list do [^item] endfor %]
> is  probably better style.

I'd already pointed this out to David!

Terry
==============================================
Terry Dartnall
School of Computing and Information Technology
Griffith University Nathan Brisbane
Queensland 4111 Australia
Phone:  61-7-3875 5020
Fax:    61-7-3875-5051
==============================================

Sun, 10 Feb 2002 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages