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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:28:24+00:00 2026-05-28T18:28:24+00:00

I am a bit confused about this function definition in Prolog: sample(X,[X|Tail]) :- member(X,Tail).

  • 0

I am a bit confused about this function definition in Prolog:

sample(X,[X|Tail]) :- member(X,Tail).

this function checks if X is at the first position of the given list and if X is also found in the tail of the list.

sample(1,[1,2,3]).
false.
% because 1 is not found in the tail

sample(1,[1,2,1]).
true.

But how does it work? X is the parameter given by the user but it seems to be overwriten by the head|tail extraction from the list. So it seems X new value is the first element in the list.

  • 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-05-28T18:28:25+00:00Added an answer on May 28, 2026 at 6:28 pm

    You’re thinking too hard. In the first case:

    sample(1, [1,2,3])
    

    Prolog is able to bind X, because 1 is 1:

    sample(1, [1,2,3])
    X=1, Tail=[2,3]
    

    Prolog then checks the body of the clause:

    member(X, Tail) -> member(1, [2,3])
    

    This is obviously false, so sample(1, [1,2,3]) doesn’t succeed.

    In the next case, the binding still works, but Tail is bound to something else:

    sample(1, [1,2,1])
    X=1, Tail=[2,1]
    

    So we check the body:

    member(1, [2,1])
    

    which is obviously true, so sample(1, [1,2,1]) succeeds.

    Now, if you were to try this call:

    sample(1, [2,3,1])
    

    You’ll still get false, because in this case, 1 isn’t equal to 2, so there’s no way to bind X at the beginning, so the body never gets checked. sample(1, [2,3,1]) is false even though 1 appears in both places, because it didn’t appear at the beginning of the list as well.

    I have no idea why you’d want such a function, but it is working as one would expect. There’s no overwriting of variables happening here.

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

Sidebar

Related Questions

I am a bit confused about this. If you're building a distributed application, which
I am getting myself a bit confused about how to go about this. My
This should be obvious, but I'm getting a bit confused about PHP variable scope.
I am a bit confused about this piece of code. public static void Foo(A
I'm a little bit confused about how to approach this. I had never used
This might be a very basic question, but I am a bit confused about
I am a bit confused about this. I have an R package that has
I'm a bit confused about the result of this code : #include <stdio.h> #include
I'm confused about this bit of code from the HTTP package : type HandlerFunc
I'm a little bit confused about how Haskell dispatches to the correct function in

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.