[Bug 8662] [XQuery 1.1] Need an easy way to define a recursive inline function

http://www.w3.org/Bugs/Public/show_bug.cgi?id=8662





--- Comment #7 from Pavel Minaev <int19h@gmail.com>  2010-02-05 21:39:19 ---
(In reply to comment #6)
> I've written some analysis and a proposal, available here:
> 
> http://lists.w3.org/Archives/Member/w3c-xsl-query/2010Feb/0015.html
> http://jpcs.posterous.com/adding-recursive-inline-function-to-xquery-11

There are some uncertainties in the syntax. For one thing, the suggested syntax
looks like a variable binding, complete with $ - but then functions are called
without using $, as if they were true local functions and not variables:

   local $even := ...
   ...
   even(abs($a) - 1)

Also, the syntax itself is, IMO, too generic-looking while being specialized.
In particular, keyword "local", on one hand, does not in any way indicate the
main difference from "let" (scope of bound identifiers), and on the other hand,
looks like a general-purpose local variable declaration, even though it cannot
actually be used as such.

One idea would be to allow "letrec" (given that XQuery syntax is traditionally
verbose, "let recursive" looks more in-line with the existing language) as
described in point #3, but restrict initializer to inline function on grammar
level. Such a restriction would fully enable this particular use case, and also
allow for further extensions, if they are deemed desirable (e.g. mutually
recursive lazy sequences a la Haskell, etc). It is also rather
self-descriptive. So:

  let recursive
    $even := function($a)
    {
      if($a == 0) then true()
      else $odd(abs($a) - 1)
    },
    $odd := function($a)
    {
      if($a == 0) then false()
      else $even(abs($a) - 1)
    }
  return ($even, $odd) 

is fine, but neither:

   let recursive $x := $y, $y := $x

nor

   let recursive $x := 1, $y := 2

is not, because the expression must be an inline function.

If this syntax is still too misleading with respect to the limitation (less
generic than it seems to be), then why not be fully explicit?

  let recursive function
    $even($a)
    {
      if($a == 0) then true()
      else $odd(abs($a) - 1)
    },
    $odd($a)
    {
      if($a == 0) then false()
      else $even(abs($a) - 1)
    }
  return ($even, $odd) 


-- 
Configure bugmail: http://www.w3.org/Bugs/Public/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

Received on Friday, 5 February 2010 21:39:21 UTC