I want to empty a used variable in Prolog.
For example, I want to do like this:
i = null;
but in Prolog.
How do I do this?
You cannot do that with pure prolog variables; once a variable is instantiated its value cannot change.
You could use global variables, but I strongly recommend to try to think a less imperative way to write the program.
edit: check DaveEdelstein's comment too
Prolog has assign-once variables. That is any particular variable in Prolog can only ever get one value assigned to it. A Prolog variable at any point in execution either has a value, which can thereafter never be changed.
Related
Let's say I'm running a bash script under set -u. Obviously, for any given variable, I need to ensure that it's set. Something like:
foo=
However, if I want to keep any pre-existing value that might be set by my caller, this would overwrite it. A simple solution to this problem is to do this instead:
: ${foo:=}
But I have some code that does this (more complicated) way:
foo=${foo+$foo}
Now, I know this second way works. My question is, is there any advantage to it over the first way? I am assuming there is but now can't remember what it was. Can anyone either think of an edge case (no matter how obscure) where these two constructs would behave differently, or provide a compelling explanation that they can't?
I can't think of any case where they would differ. They're just alternative logic for the same thing.
The meaning of the simple solution is: If foo is unset/empty, set it to the empty string.
The meaning of your code is: If foo is set, set it to itself, otherwise set it to an empty string.
Your code seems like more work -- why set something to itself? Just do nothing and it will keep its value. That's what the simpler version does.
You can also simplify the simple solution further by removing the : in the parameter expansion.
: ${foo=}
This makes it only test whether foo is unset. If it's set to the empty string, no default needs to be assigned.
My question is, is there any advantage to it over the first way?
Maybe this is subjective, but one advantage is that it clearly looks like a variable assignment. Anyone who sees the command foo=${foo+$foo} will immediately understand that it sets the variable foo (even if they need to look up the ${parameter+word} notation to figure out what it sets it to); but someone who sees the command : ${foo:=} is likely to completely miss that it has the side-effect of modifying foo. (The use of : is definitely a hint that something might be happening, since : itself does nothing; but it's not as blatant.)
And of course, someone searching the script for foo= will find the former but not the latter.
That said, I would personally write this as either foo="${foo-}" or foo="${foo:-}", which still makes clear that it sets foo, but is a bit simpler than foo=${foo+$foo}. I also think that readers are more likely to be familiar with ${parameter-word} than ${parameter+word}, but I haven't asked around to check.
I've been searching around a bit for way to store variables as a list for future use in different methods. So say one method produces X, another method has one produces Y and so on, I don't know if there is a way to declare a list, append each variable to that list and than call it at the to output everything I've saved in it.
Hope this makes sense. Any help would be great, thanks
You can use assert/1 to store things, like
assert(data(100))
then you can just say
data(X)
later to get X = 100
It sounds like what you want is to have each module call assert with a different predicate.
You can use lists also, but it will probably not be better in any way.
Addition: it is customary in this situation to use predicate names that are unlikely to be used in the interpreter, e.g. ones containing spaces, as in
assert('My Data from Module X'(100,200,300))
I need to refer to the same variable in several functions in a python script. (The write context of a CGPDFDocument).
I can either write global writeContext in every function;
or add the variable as an argument to every function;
....both of which seem to be excessive duplication.
Other answers to questions about global variables in python suggest that they are "bad".
So what's the better way of handling it?
In the freemarker template language, I can test whether a variable exists by using constructs like variable?exists or variable??. I can also cause a previously non-existent variable to exist by assigning to it, e.g., <#assign variable = "hi" />. But how can I cause a previously existing variable to no longer exist?
I have some other dude's freemarker template, with logic at various points that tests for the (non-)existence of certain variables. In my use case, it would be simplest if I could have a variable that exists at one point, then becomes undefined when including his template, then gets assigned to again later on. The alternative is to restructure things more significantly.
No, there's no directive fort that. Maybe it can be achieved with a custom directive (TemplateDirectiveModel) that can then write null into the variable through the Environment. (Unless the Environment API checks for null-s...)
If I say:
x = "abc"
this seems like a declaration, definition and assignment, all at the same time, regardless of whether I have said anything about x in the program before.
Is this correct?
I'm not sure what the correct terminology is in Ruby for declarations, definitions and assigments or if there is even a distinction between these things because of the dynamic typing in Ruby.
#tg: Regarding your point # 2: even if x existed before the x = "abc" statement, couldn't you call the x = "abc" statement a definition/re-definition?
Declaration: No.
It doesn't make sense to talk about declaring variables in Ruby, because there's nothing analogous to a declaration in the languages. Languages designed for compilers have declarations because the compiler needs to know in advance how big datatypes are and how to access different parts of them. e.g., if I say in C:
int *i;
then the compiler knows that somewhere there is some memory set aside for i, and it's as big as it needs to be to hold a pointer to an int. Eventually the linker will hook all the references to i together, but at least the compiler knows it's out there somewhere.
Definition: Probably.
A definition typically set an initial value for something (at least in the familiar compiled languages). If x didn't exist before the x = "abc" statement, then I guess you could call this a definition, since that is when Ruby has to assign a value to the symbol x.
Again, though, definition is a specific term that people typically use to distinguish the initial, static assignment of a value to some variable from that variable's declaration. In Ruby, you don't have that kind of statement. You typically just say a variable is defined if it's been assigned a value somewhere in your current scope, and you say it's undefined if it hasn't.
You usually don't talk about it having a definition, because in Ruby that just amounts to assignment. There's no special context that would justify you saying definition like there is in other languages.
Which brings us to...
Assignment: Yes.
You can definitely call this an assignment, since it is assigning a value to the symbol x. I don't think anyone will disagree with that.
Pretty much. And if, on the very next line, you do:
x = 1
Then you've just re-defined it, as well as assigned it (its now an integer, not a string). Duck typing is very different to what you're probably used to.