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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:32:17+00:00 2026-06-19T03:32:17+00:00

I am having trouble understanding the codes below. Can someone explain step by step

  • 0

I am having trouble understanding the codes below.
Can someone explain step by step what is happening if I have the follow input:

append([1,2,3], Lst).

Actually, I don;t get how 1 and 2 is appended the list Lst as a result.

append([_], []).    
append([H|T], [H|N]) :- append(T,N).
  • 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-19T03:32:19+00:00Added an answer on June 19, 2026 at 3:32 am

    It sounds like you’re new to Prolog. If so, welcome! Let’s analyze this.

    This unfortunately-named function has two clauses. Prolog looks at the clauses in order to see which one applies. When it finds one that matches, it tries to perform it. If there’s a failure somewhere in performing it, it will back up and try the next option. Where precisely these choice points are varies depending on the program; in this program, the only one will be at the clause level, deciding which rule to use.

    One way of looking at the first rule is that it is saying “A list with one element, regardless of what that element is, is related to the empty list.” Looking at append([_], []), if we had X = [foo] and Y = [], it would hold, because [foo] is a one-item list and [] is the empty list. This rule is good Prolog style, because it will work regardless of instantiation: we could supply the left or the right or neither, it doesn’t matter.

    The second clause is also quite simple. It says that the left argument and the right argument are related if they both start with the same item, and if the rest of the lists are also related by this same predicate. In other words, if I have two lists X and Y such that append(X, Y) is true, then append([H|X], [H|Y]) is also true. It doesn’t matter what H is and it doesn’t matter what X and Y are, except insofar as would be implied by append/2.

    Thinking logically, if I know that any one-item list is related to the empty list, and any list is related to a list that starts with the same item and otherwise is the same, the only kinds of lists that can be so related are lists where every item is the same, except the left list has one more item in it at the end which is not present on the right. So [1,2,3,4] is related to [1,2,3], but so is [1,2,3,foo] and [1,2,3].

    Procedurally, let’s look at what happens as this predicate is processed with this set of arguments:

    append([1,2,3], X).
    

    The first rule won’t match on [1,2,3]. So we must look at the second rule:

    append([1|[2,3]], [1|X]) :- append([2,3], X).
    

    We can repeat:

    append([2|[3]], [2|Y]) :- append([3], Y).
    

    Now the first rule does match:

    append([3], []).
    

    So putting it all together:

    append([1,2,3], [1|X]) implies
      append([2,3], X=[2|Y]) implies
        append([3], Y=[])
      so Y = []
    so X = [2]
    so the right side is [1,2].
    

    A Prolog trace will show you basically the same information:

    ?- trace, append([1,2,3], X).
       Call: (7) append([1, 2, 3], _G1633) ? creep
       Call: (8) append([2, 3], _G1752) ? creep
       Call: (9) append([3], _G1755) ? creep
       Exit: (9) append([3], []) ? creep
       Exit: (8) append([2, 3], [2]) ? creep
       Exit: (7) append([1, 2, 3], [1, 2]) ? creep
    

    What makes this Prolog code confusing is that it doesn’t look like you’ve told Prolog how to do anything. And it’s true, you haven’t, but by specifying what is true logically, Prolog is able to figure it out by itself. This is pretty clever code. If this were Haskell, we’d be talking about the built-in function init, which returns all of the list but the last item.

    Hope this helps!

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

Sidebar

Related Questions

I'm having trouble understanding why the code below doesn't compile -- could someone please
I'm having trouble understanding the dynamic keyword (used .net 2 up til recently). Can
I'm having trouble understanding the rules behind argument-dependent (Koenig) lookup. Consider the code below:
I'm having a bit of trouble understanding why the below code is not working:
I'm having trouble understanding what's really happening with the code in a book I'm
I'm having trouble understanding why the code below #include <string> #include <vector> #include <map>
I'm trying to understand the code below, and I'm having some trouble understanding why
I have been looking at this code and I am having trouble understanding the
I am having trouble understanding how the System Registry can help me convert a
Below is come code that I am having trouble understanding. The output is 13

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.