Re: SICP's exercise 4.68



s_lesha@xxxxxxx writes:
>(assert! (rule (reverse () ())))
>(assert!
> (rule
> (reverse ?list1 ?list2)
> (and (append-to-form (?x) ?tail ?list1)
> (append-to-form ?head (?x) ?list2)
> (reverse ?tail ?head))))
>
>As for me it looks ok, but stick on (reverse (a) ?x).

Your program is logically correct, but the query system's algorithm isn't quite
smart enough to solve it. I think the problem comes in the clause
> (append-to-form ?head (?x) ?list2)
When this clause is seen, in your example query, the system knows that ?x = a,
but there are no constraints on ?head or ?list2. So, this sub-query matches
(append-to-form (?e1) (a) (?e1 a))
or
(append-to-form (?e1 ?e2) (a) (?e1 ?e2 a))
and so on. Now, as it turns out, the REVERSE clause will eliminate most of
those matches, but the system has to test them all.

The trick is to avoid generating infinitely many matches to sub-queries.
One way to do that is to define a reverse-helper-with-length relation that
includes the two lists and a third list whose only purpose is to be the same
length as those. If you do it right, most matches of sub-queries will be
known to be too long before they get you in trouble.

The most elegant solution I've seen involves a ZIP relation like this:
(zip (a b c) (d e f) ((a d) (b e) (c f)))
With this, and a FORWARD-REVERSE that only works in one direction, you can make
a REVERSE that works in both directions. This still really involves counting,
since ZIP ensures that all three of its list components have the same length.
.



Relevant Pages

  • Re: how do i get a query to work?
    ... A Query is a standardized way to select one or more fields, ... The SELECT clause is simply a list of the fields that you want to ... The FROM clause lists the tables involved in the query and specifies ... or you could have a much more complex one applying criteria to ...
    (microsoft.public.access.gettingstarted)
  • Re: Maximum number of elements ina an IN() Clause
    ... Remote table has 856,000 rows. ... would making the passtrough query WITHOUT the in clause and then, ... I have successfully used the INclause for smaller lists, ...
    (microsoft.public.access.queries)
  • Re: passing variables into an ORDER BY clause
    ... (Reverse address to reply.) ... "studen77" wrote in message ... One of these parameters I want to use in an ORDER BY clause within the query ...
    (microsoft.public.sqlserver.programming)
  • Re: Problems when using TOP statement
    ... Can you provide DDL, the *exact* query ... See http://www.aspfaq.com/5006 for help on DDL. ... > When I added an ORDER BY clause the times became 2 seconds when using TOP ... >> (Reverse address to reply.) ...
    (microsoft.public.sqlserver.programming)
  • Re: Random lists PRINT identical items daily
    ... The query does generate random ... numbers and created new lists each time but the REPORT that pulls the ... information from the query prints the same lists during each session of ... your HAVING clause should really be a WHERE clause. ...
    (microsoft.public.access.forms)