Home > Archive > Prolog > January 2006 > Global variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| decipher.One 2005-12-14, 7:04 pm |
| Sorry I'm rather new to prolog and was wandering if it was possible to
make a global variable in sicstus prolog.
| |
| Matthew Huntbach 2005-12-16, 3:57 am |
|
On Wed, 14 Dec 2005, decipher.One wrote:
> Sorry I'm rather new to prolog and was wandering if it was possible to
> make a global variable in sicstus prolog.
Your desire to make a global variable almost certainly means you haven't
started thinking in Prolog.
Matthew Huntbach
| |
| Bart Demoen 2005-12-16, 7:58 am |
| Matthew Huntbach wrote:
>
>
> On Wed, 14 Dec 2005, decipher.One wrote:
>
>
>
> Your desire to make a global variable almost certainly means you haven't
> started thinking in Prolog.
>
> Matthew Huntbach
And to complement Matthews reply: the answer to your question is yes.
Why do you want it ?
Cheers
Bart Demoen
| |
| Walter 2006-01-10, 4:10 am |
| To complement Bart's reply, in imperative languages people use global
varibles to do things like accumulate answers (eg to count things), or to
make information available to procedures without having to pass the
information to every procedure.
In Prolog, accumulators are generally passed down the recusion chain, so for
example if you were counting items in a list you might have:
countit([],0). %a fact about how many elements are in an empty list
countit([H|T], Count) :- countit(T, TCount), Count is 1 + TCount.
Globally known information is generally kept as facts during a computation.
Prolog's "Negation as Failure" processing (ie a goal fails if it can't
explicitly succeed) opens up a middle ground, where you discover some fact
during processing that you didn't know before and want to pass it on. In
general, this is done by constructing a data structure that is passed from
procedure to procedure as needed -- ie no global variable. However, there
are times when using the assert/1 built-in is practical.
I would warn you against indiscriminant use of assert and retract, though,
since it can make your programs logic difficult to perceive.
Walter
"Bart Demoen" <bmd@cs.kuleuven.ac.be> wrote in message
news:1134728583.555919@seven.kulnet.kuleuven.ac.be...
> Matthew Huntbach wrote:
>
> And to complement Matthews reply: the answer to your question is yes.
> Why do you want it ?
>
> Cheers
>
> Bart Demoen
|
|
|
|
|