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

  • Home
  • SEARCH
  • 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 9176627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:08:50+00:00 2026-06-17T17:08:50+00:00

I’m trying to create a predicate which is true if the third list is

  • 0

I’m trying to create a predicate which is true if the third list is composed of T and F values, depending on the fact whether two elements of same index from the first and the second list are equal. A query like truth_list(['abc','def'],['zui','def'],L). should give L=['F','T'].

Here’s my try:

truth_list([],[],_).
truth_list([H1|T1],[H2|T2],TL):-
    (H1==H2)->(H3='T');(H3='F'),
    Temp=TL,
    TL=[H3|Temp],
    truth_list(T1,T2,TL).

I’d be grateful if someone could offer an explanation why this doesn’t work as expected.

  • 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-17T17:08:52+00:00Added an answer on June 17, 2026 at 5:08 pm

    So we’re all on the same page, when I ran the query I got this result:

    L = ['F'|L].
    

    So the first thing that comes to mind is that you’re probably reusing a variable, and indeed, the following clauses look suspect:

    Temp=TL,
    TL=[H3|Temp],
    

    Variables in Prolog aren’t really “assignables” as in other languages; all you can really do is establish a binding, so the above code is the same as saying this:

    TL=[H3|TL]
    

    Which is why our result looks like it does: L=[‘F’|L]. I rewrote the body slightly to fix the problem and wound up with this code:

    truth_list([H1|T1],[H2|T2],[H3|TL]):-
        (H1=H2 -> H3='T' ; H3='F'),
        truth_list(T1,T2,TL).
    

    You need to have the parentheses around the whole condition or you’ll get strange behavior. From there, I just removed the unnecessary TL bindings (they can’t work anyway because variables in Prolog are not assignables). It turns out once you fix these problems you’ll find another problem, which is that you’ll get this:

    L = ['F', 'T'|_G297].
    

    If it’s not obvious, this is because your base case is too vague and should instead look like this:

    truth_list([], [], []).
    

    So the final corrected version looks like this:

    truth_list([],[],[]).
    truth_list([H1|T1],[H2|T2],[H3|TL]):-
        (H1=H2 -> H3='T' ; H3='F'),
        truth_list(T1,T2,TL).
    

    This is usually where @false shows up and points out that we have a problem using the predicate with different instantiations, so let’s check that now and avoid some fury:

    ?- truth_list(['abc','def'],['zui','def'],['T','F']).
    false.
    ?- truth_list(['abc','def'],['zui','def'],['F','T']).
    true.
    ?- truth_list(['abc','def'],['zui','def'],['F','T','T']).
    false.
    ?- truth_list(['abc','def'],['zui','def','def'],['F','T','T']).
    false.
    

    These all look alright, so it doesn’t look like we’re hallucinating lies when all the arguments are instantiated. That’s good. Now let’s check partial instantiations:

    ?- truth_list([X, 'def'], ['abc', Y], ['T', 'T']).
    X = abc,
    Y = def.
    

    Cool, that worked.

    ?- truth_list([X, 'def'], ['abc', Y], ['F', 'T']).
    false.
    

    Eh. Well, it looks like Prolog doesn’t know to hallucinate some other binding for that 'F' value. Not sure if that’s a problem or not but I don’t see an obvious solution. That means the following probably won’t work:

    ?- truth_list(X, Y, ['T', 'T']).
    X = Y, Y = [_G296, _G302].
    

    Surprising, but it actually did work, assigning to both the same list of two unknowns. Cool. I think we’re in good shape.

    Edit: let’s incorporate @false’s improvement. Then we get the following:

    truth_list([], [], []).
    truth_list([H1|T1], [H2|T2], [H3|TL]) :-
      (H1 = H2, H3 = 'T' ; dif(H1,H2), H3 = 'F'),
      truth_list(T1, T2, TL).
    

    Now we get the desired behavior:

    ?- truth_list([X, 'def'], ['abc', Y], ['F', 'T']).
    Y = def,
    dif(X, abc) ;
    

    So Prolog has inferred that Y is ‘def’, and concluded that X is at least not ‘abc’, so this is an improvement.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.