qexo-general
[Top][All Lists]
Advanced

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

[Qexo-general] Iterating over Kawa lists


From: Jocelyn Paine
Subject: [Qexo-general] Iterating over Kawa lists
Date: Wed, 10 Jul 2002 10:10:35 +0100 (BST)

Can one iterate through a list using 'for'? 

I'm still using Kawa for most of my project, but decided to have a go at
using Qexo for all the output (most of my output is in HTML). Not only
would this be a lot shorter, but Qexo is an interesting new toy, and a
relief from Scheme's brackets. So I decided to write a suite of Qexo
functions that would accept my Kawa data structures and convert them to
HTML. These structures are such things as sequences implemented as lists,
graphs implemented as lists, and ... lots of other things implemented as
lists.

So I wrote a few functions, and they gave some very strange output. So I
decided to experiment. Would you not expect that the "obvious" Kawa-style
action for 'for' to perform on a list is to iterate over the list's
elements?

Indeed 
  {--1--} for $x in list(1,2,3,4) return $x
returns 
  <list>1 2 3 4</list>
which looks plausible.

But not for long. 
  {--2--} for $x in list(1,2,3,4) return 2 * $x
throws an illegal argument exception rather than returning a sequence of
four even numbers.

Bouncing more probes off the surface of the TLI shows that 'for' regards
the list as one item, not four. E.g.:
  {--5--} for $x in list(1,2,3,4) return cadr($x)
  2
instead of complaining that $x is not a list.

So I have to try something else. Since list(1,2,3,4) prints as 
  <list>1 2 3 4</list>
it is presumably the same variety of beast as
  <foo>1 2 3 4</foo>

But whereas  
  invoke( <list>1 2 3 4</list>, "getClass" )
  invoke( <foo>1 2 3 4</foo>, "getClass" )
both display gnu.mapping.Values (a subclass of TreeList),
  invoke( list(1,2,3,4), "getClass" )  
displays gnu.lists.Pair .

So even though list(1,2,3,4) displays as <list>1 2 3 4</list> , it isn't
one. The top-level interpreter is lying!

(Otherwise, I would have been able to extract the text between the tags by
doing the moral equialent of
  {--8--} ( <list>1 2 3 4</list> ) / text()
  1 2 3 4
)

So, anyone, can I persuade 'for' to look inside a pair before it begins
iterating? Or do I have to resort to recursion?

Jocelyn Paine
http://www.ifs.org.uk/~popx/
+44 (0)7768 534 091 






reply via email to

[Prev in Thread] Current Thread [Next in Thread]