I want to append to a list a variable, N, that’s bound to a number.
N = 1.
append([N], [2,3,4], Z).
Z = [N,2,3,4]. //Wrong output!
I want to get Z = [1,2,3,4]
How do I append the number part of a variable, not the actual variable itself?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
I’m afraid Prolog doesn’t have variable assignment like you’re used to, just variable binding. So the “statements”
actually constitute two completely unrelated queries. Fortunately, the effect you desire can be achieved by combining your queries:
If you truly need a global variable, you can always use a fact or
asserta/1to define one dynamically.Also note: in the future, you’ll probably want to make sure you use
isinstead of=when dealing with numbers.