Lucas Maine
Lucas Maine

Reputation: 19

How does declaring variables in swi prolog work?

Sorry for the inconvenience, I'm still new to Prolog's syntax, Im just asking how would one declare/instantiate and manipulate a variable X (if possible) and be able to write/print it out similar to other languages. For example:

int x = 5;
print(x);

or even swapping variables

int x = 5, y = 10, z;
z = y;
x = z;
y = x;

Is it possible to implement these in prolog? If so, how? If not, why?

Upvotes: 1

Views: 293

Answers (1)

TA_intern
TA_intern

Reputation: 2436

You are not the first person to ask this exact question. Even searching around on Stackoverflow would have been easier. By now I suspect there is a CompSci professor out there just trolling their students with this question.

Your example looks like C or a C-like language. Other very popular languages do not require declarations, for example Python. Declaring variables is an artifact of early compiled languages. This is a very broad topic that belongs to a university-level lecture on compiler design, programming language design and so on.

You "swap variables" when your variables are handles to registers in the processor or memory locations. Prolog is on a very different level, to the point that I feel like saying "unask the question" despite not being too wise.

So, let's start again: what are you trying to achieve by swapping variables?

If you want to print a 5, write:

?- X = 5.
X = 5.

or maybe:

?- write(5).
5
true.

and so on and so forth. Long story short, there is more to this question than meets the eye, but I am not convinced this is the right place to ask it.

Upvotes: 2

Related Questions