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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:30:17+00:00 2026-06-16T06:30:17+00:00

Before this program I’ve seen in the following example that say if an element

  • 0

Before this program I’ve seen in the following example that say if an element X belongs to a String L.

The code of this example is the following:

member2(X, [X|_]).
member2(X,[_|T]):- member2(X,T).

I have one fact that represent the base case (the X element belongs to the list if it is in the head of the list) and the rule is used to recursively search in the tail of the list if the element X is not the head…

This version was pretty obscure for me and someone advised me to see it in this way to understand how it work:

member2(X, [X|_]).
member2(X,Y):- Y = [_|T],
               member2(X,T).

So the meaning is more clear:

I have one fact that represent the base case (the X element belongs to the list if it is in the head of the list).

The rule say that I have to prove two things:

1) Y = [_|T] This is true because the anonymous variable _ unifies with anything

2) It recursively search the element X in the tail list

Ok, now I have a problem to understand this new exercise, I have to write a SWI Prolog program that, given two strings, concatenate the second string to the first in a third string

The solution is:

myappend([], L, L).
myappend([X|L1], L2, [X|L]) :- myappend(L1,L2,L).

I have some problem to understand how it work

I have understand that the fact represents the base case: when the first parameter is a void list [] and the second parameter is a not void list, the concatenation of the first parameter and second parameter is the second parameter list (and this equals to the third parameter)

The rule represents the general case in which the first parameter is not a void list…so I think that when this case occurs it delete the head of L1 parameter (the head of the first list) until this is an empty list [], so I will have the base case and the third parameter is unified to the second one…then do backtracking…

But I have problem to understand how exactly work the rule:

myappend([X|L1], L2, [X|L]) :- myappend(L1,L2,L).

I am trying to do something like the previous example and translate it in a more speaking logic form because in this way is very unclear for me….but I have not ideas how to do…how can I do

What is the logic behind this rule? what does it mean?

  • 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-16T06:30:19+00:00Added an answer on June 16, 2026 at 6:30 am
    myappend([], L, L).
    
    myappend([X|L1], L2, [X|L]) :- myappend(L1,L2,L).
    

    An longer version of the 2nd rule:

    myappend(L1, L2, Concat) :- 
        L1 = [HeadL1|RestL1], Concat = [HeadConcat|RestConcat], 
        HeadL1 = HeadConcat,
        myappend(RestL1,L2,RestConcat).
    

    The first rule means that, if the first list is empty, the concatenated list is the same as the second list.

    The second rule means that, the final list will be formed by the first element of the first list X, and a list L that is the concatenation of the rest of the first list L1 and the second list L2. Note that the first argument can only be unified only if it is a list and it is not empty, due to the constraint by [X|L1].

    You can think of the predicate as testing whether the 1st, 2nd and 3rd argument has the relation of 1st and 2nd concatenates is the 3rd argument. Since it is easy and efficient to extract head element of a list in Prolog, the solution above picks out the first element, match them ([X|L1] and [X|L]) and test that the rest of the elements satisfy the concatenation relation.

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

Sidebar

Related Questions

I have run this program before and it worked fine. Then I added the
Is this code valid C: fputc(*(Checking 7 bytes before this string-7), stdout); Is there
I have this program that at one point accesses os.getcwd(), but some times, depending
#include<stdlib.h> #include<stdio.h> #include<string.h> //This program is a sorting application that reads a sequence of
Before writing this program,I thought that our is a package scope variable and my
Before I run this program I have 0 errors or warnings. But when I
This is the code I have, please look at it before you read the
before this I wrote all jquery-code into main fail. Now I want to move
I have some date and I want to get last x day before this
I tried this before: string st = Unit; foreach (Label lb in tabControl1.Items) {

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.