recursion - Stuck with Prolog -


I just started learning Prolog One of the exercises wants me to write back / 2, a real meaning is that when the second list argument is behind the first list argument. And I have to use a paste plant to write backwards.

  Paste ([], L, L). Paste (L-, L2, L3)   

I worked for hours for this But still the answer is not answered.

What do I have here:

  Behind [H | T], LREV): - Paste ([X | T], X, LRV) backwards ([], []).    

Prologues lists are very simple data structures.

  • An empty list has been marked with atomic [] .
  • A non-empty list structure '.' / 2> where the first argument is major and the second argument is tail , the second list is not empty or empty. Has empty list.

    For non blank lists

    • [a] structure is . '(A, []) <
    • [a, b] is the structure . '(A,'. '(B, [])) <(A,'. '(B,'. '(C, []))
    • [a] , b, c] structure is .) <
    • tail] absolutely '.' (Head, tail) .
    • [one | [B, c]] is exactly [a, b, c] and t is equal to '.' (A, '.' (B, '.' (C, [])

      It is easy to see why someone may prefer a little syntax Chinese .

      The structure of a list, more like a classic link list ... traditional ... languages, makes it trivial to add and remove items from the head (left) at the end of an existing list

      One should note that if you delete items from a list at a time and add you reverse Order incentives They produce a list.

      A common proplog phrase is the use of an subsidiary or accumulator argument that governs through resurgence is often This assistant will be given preference with the initial value. Anyone can also keep in mind that most recurring problems have one or two special cases and the broad normal case. / P>

      Such a thing as such mentioned:

        paste ([], r Rs.)% Special case: If unite accumulator with the source list is empty, then the result. Paste ([X | Xs], t, rs): -% general case: t 1 = [x | T],% - prepaid the head of the current list for temporary, make a new temporary and paste (xs, t1, rs)% - recursion down. Anyone can take care that the second section above can be re-applied for simplicity to remove the apparent construction of new temporary:  
        Paste ([], Rs, Rs.)% Special case: If the source list is empty, unite the creditor with the result. Paste ([x | xs], t, rs): -% General case: Paste (xs, [x | t], rs)% - Include the current head in temporary and recursive down. . Either way, it is necessary to understand that the priority is achieved with the temporary initial blank list ( [] ).   

      Call it this way, then:

        paste ([a, b, c], [], r)   / 3  

      / Code> Distinguishing, making your behind / 2 is ingenious:

        Backwards (L, R): - Paste (L, [], R) It exposes another common projection phrase: a small "public" definition that calls a "private"  worker domination  which is actually all Working Is.   

Comments

Post a Comment

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -