qexo-general
[Top][All Lists]
Advanced

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

Re: [Qexo-general] Iterating over Kawa lists


From: Per Bothner
Subject: Re: [Qexo-general] Iterating over Kawa lists
Date: Wed, 10 Jul 2002 08:55:50 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1a) Gecko/20020610

Jocelyn Paine wrote:
Can one iterate through a list using 'for'?

No.  A Qexo sequence is is the same as Scheme "multiple values", not a
Scheme list.  The reason for this is that an XQuery sequence has very
different semantics from Scheme lists:  You cannot nest XQuery
sequences, and a sequence of a single value is the same as just that
single value. On the other hand, you can nest lists, and a list of just
a single value is very different from that value itself.

Because of this difference, I don't see any clean way to automatically
convert between sequences and lists.  A Scheme list is a single value,
and this a sequence of length one.

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?

Obvious but wrong.

  {--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.

That is because the whole list is a single-valued sequence, the for only
gets a single iteration, and you try multiplying an integer and a list.

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.

Yes.

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>

Yes and no.  I just use <list>1 2 3 4</list> as the most reasonable
"XML syntax" for a Scheme list.

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),

gnu.mapping.Values is how Scheme multiple values are represented in
Kawa, and also how Qexo sequences are represented.  There are reasons
that Values imherits from TreeList, but don't get too hung up on it.

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.

It isn't one what?  It is a Scheme list, but not a TreeList.

The top-level interpreter is lying!

Smile when you say that ...

(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
)

How Qexo operations such as string-value and children work on values
that are not in the XQuery value space is undefined.  We can extend
these operations to be useful for Scheme lists, but there are different
ways to do so, and the cleanest extension may not be the "obvious" one.

My intuition is that children applied to a Scheme list should return
the elements of the sequence.  More generally for objects that implement
gnu.lists.Sequence, which also includes Scheme vectors and strings.
That looks like a relatively simple change.
--
        --Per Bothner
address@hidden   http://www.bothner.com/per/




reply via email to

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