Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7946687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:13:20+00:00 2026-06-04T01:13:20+00:00

This is an algorithm to append together two lists: Domains list= integer* Predicates nondeterm

  • 0

This is an algorithm to append together two lists:

Domains
list= integer*

Predicates
nondeterm append(list, list, list)

Clauses
append([], List, List) :- !.
append([H|L1], List2, [H|L3]) :- append(L1, List2, L3).

Goal
append([9,2,3,4], [-10,-5,6,7,8], Ot).

The result is a list [9,2,3,4,-10,-5,6,7,8], and it’s saved in “Ot“.

My question is, how does this work?

What I understand is that in every recursive call, in the first list, you get only the tail as a list ( thus reducing its size by 1 until it’s [] ), the second argument “List2” does not change at all, and the 3rd argument … at first it’s [], and after each recursive call you get its tail, but since it’s [], it stays [].

So how come, suddenly, in 3rd argument (“Ot“) we have the appended list ?
Can someone explain this algorithm step by step ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:13:21+00:00Added an answer on June 4, 2026 at 1:13 am

    First, let’s translate the clauses into something more understandable:

    append([], List, List) :- !.
    

    can be written

    append([], List2, Result) :-
        Result = List2,
        !.
    

    and

    append([H|L1], List2, [H|L3]) :- append(L1, List2, L3).
    

    can be written

    append(List1, List2, Result) :-
        List1  = [Head1 | Tail1],
        Result = [HeadR | TailR],
        Head1  =  HeadR,
        append(Tail1, List2, TailR).
    

    I hope this will already be clearer for you.

    Then, step by step, the number indicates the clause used each time, and the resulting call is shown:

    append([9, 2, 3, 4], [-10, -5, 6, 7, 8], Ot).
    |
    2
    |
    ` append([2, 3, 4], [-10, -5, 6, 7, 8], Ot'). % and Ot = [9|Ot']
      |
      2
      |
      ` append([3, 4], [-10, -5, 6, 7, 8], Ot''). % and Ot' = [2|Ot'']
        |
        2
        |
        ` append([4], [-10, -5, 6, 7, 8], Ot'''). % and Ot'' = [3|Ot''']
          |
          2
          |
          ` append([], [-10, -5, 6, 7, 8], Ot''''). % and Ot''' = [4|Ot'''']
            |
            1
            |
            ` Ot'''' = [-10, -5, 6, 7, 8]
    

    At this step all the values we’re interested in are already defined. Notice how the head of the result is set before its tail is filled up by a subsequent (tail recursive) call to append, building the resulting list in the characteristic for Prolog top-down fashion (also known as “tail recursion modulo cons”).

    Let’s follow the definitions to see what Ot is, at the final step:

    Ot = [9|Ot']
            Ot' = [2|Ot'']
                     Ot'' = [3|Ot''']
                               Ot''' = [4|Ot'''']
                                          Ot'''' = [-10, -5, 6, 7, 8]
                               Ot''' = [4,          -10, -5, 6, 7, 8]
                     Ot'' = [3,         4,          -10, -5, 6, 7, 8]
            Ot' = [2,        3,         4,          -10, -5, 6, 7, 8]
    Ot = [9,       2,        3,         4,          -10, -5, 6, 7, 8]
    

    I hope you’ll get something out of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this algorithm in Java to store passwords in database. I'd like to
I have this algorithm that I want to implement on VB6. Sub Main() dim
I saw this algorithm that will take numbers or words and find all possible
How to make this algorithm better and working, where I am always ending up
I am trying to port this algorithm to clojure. My code is (defn calc-iterations
Homing/Chasing Algorithm. I've been working on this algorithm, for my game. the only chase
I am trying to code this algorithm. I am stuck in the part of
Does anyone knows why after I apply this algorithm in c++ to reduce the
In python how can i develop this algorithm to find common patterns in a
I am writing a class to implement an algorithm. This algorithm has three levels

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.