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 8247629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:54:48+00:00 2026-06-07T22:54:48+00:00

How do I append lists in Prolog? I’ve searched on the Internet and I

  • 0

How do I append lists in Prolog? I’ve searched on the Internet and I found this (from http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html)

append([X|Y],Z,[X|W]) :- append(Y,Z,W).  
append([],X,X).

So it gets the Z by removing the elements of [X|Y] in [X|W]. But how do I append two lists together?

Example,

appendlist([1,2],[3,4,5],X).

The result will be X = [1,2,3,4,5].

Also I don’t know what happening in the recursion. (I traced it but didn’t understand)

EDIT: What I want to know is how it should be coded to function like the predefined append() in Prolog.

  • 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-07T22:54:49+00:00Added an answer on June 7, 2026 at 10:54 pm

    The code as you’ve posted it is (almost) OK. The order of clauses just needs to be swapped (in order to make this predicate definition productive, when used in a generative fashion):

    append( [], X, X).                                   % (* your 2nd line *)
    append( [X | Y], Z, [X | W]) :- append( Y, Z, W).    % (* your first line *) 
    

    This defines a relationship between the three arguments, let’s say A, B and C.

    Your first line says, " C is the result of appending A and B if A and C are non-empty lists, they both have the same head (i.e. first element), and the tail of C is the result of appending the tail of A with the same 2nd argument, B".

      a        a
      ----------
      b        b
      c        c
      .    d   d
           e   e
           .   .
    

    Or from left to right:

             a | b c .
               |     d e .
             a | b c d e .
    
    append(         [], 
                     Z,
                     Z ).       
    append( [X | Y   ],
                     Z,
            [X |         W ] ) :- append(
                 Y,  Z,  W).
    

    Think about it, it makes perfect sense. What it does is, we want to define the append/3 relationship, and we know what we want it to be, so we just write down some obvious facts about it that we want it to fulfill, the laws that it must follow if you will.

    So assuming we have this code already defined for us, what laws must it follow? Obviously, appending a tail of some list with another list gives us a tail of result of appending the full list with that 2nd list.

    This defines how we "slide along" the first list. But what if there’s nowhere more to slide? What if we’ve reached the end of that list? Then we’ve arrived at the empty list, and appending an empty list with another list gives us that list as the result. Obviously. And that’s what that 2nd line in your code is telling us, it says, "appending an empty list with another list produces that list as the result".

    Amazingly, having written down these two laws that append/3 must follow, is the same as writing down the definition itself.

    addition: this explains it from a declarative point of view; do check out an answer by m09 which shows it more from the operational point of view.

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

Sidebar

Related Questions

This is an algorithm to append together two lists: Domains list= integer* Predicates nondeterm
Possible Duplicate: Python list append behavior Why does this code: x = [[]]*3 x[0].append('a')
I have a list to which I append Axes3D plots. Like this: self.myList.append(self.axes.plot(xValues, yValues,
I'm using javascript to duplicate a couple select lists and append them to another
I wish to compare two lists. Generally this is not a problem as I
A simple append function like this (in F#): let rec app s t =
I'm trying to find a way to append a list to all lists within
I'm writing something in prolog and the way I used append, it ends up
I want to append 3 lists or more at once in a single expression.
I have a prolog homework, which should work like this: singles([1,2,3,2,2,4,1], [3,4]). true Now

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.