I realize this is very basic, but I could not work this out from Prolog tutorials, so I hope someone here could help me solve my problem.
I have a term which is true if one of several conditions applies:
answer -->
Var,
a([Att1],[Var1]),
a([Att2],[Var2]),
a([Att3],[Var3]),
{
[one, two, three] = [Att1, Att2, Att3] -> Var1 = Var; % first condition
[one, three, two] = [Att1, Att2, Att3] -> Var1 = Var; % second condition
[two, one, three] = [Att1, Att2, Att3] -> Var2 = Var; % third condition
[three, one, two] = [Att1, Att2, Att3] -> Var2 = Var; % fourth condition
}
All Attributes and Variables come with a fixed value, and I want to offer "answer", if any of the conditions in the "{}" section are fulfilled - but for some reason, it doesn't work. The thing is, if I just check for one of the conditions, say, the first, it works as expected. But I don't want to copy/paste the rule 4 times because I didn't get a logical "or" to work properly.
Just to put it into words, in case I coded something completely different, the first condition is meant to say: check if Att1 is equal to one and Att2 is equal to two and Att3 is equal to three. If that is the case, also confirm that Var1 is equal to the value in Var. If not, check if any of the other conditions can be resolved.
edit: Turns out I just had a ';' too much in the code.
Related
I was just wondering if there is a special way of saying when something equals something. For example in python, if you declare something equals 2, you say something = 2, whereas when you check if something equals something else, you would say:
if something == somethingelse:
So my question is in pseudocode for algorithms if I'm checking to see if a entered password equals a stored password in an IF THEN ELSE ENDIF loop, would I use one or two equal signs:
WHILE attempts < 3
Get EnteredPassword
**IF EnteredPassword = StoredPassword THEN**
Validated = TRUE
ELSE
attempts = attempts + 1
ENDIF
ENDWHILE
Usually, pseudocode is very broad and every author has their own way of expressing it. As
Aziz has noted, usually x <- 1 is used for an assignment and x := x + 1 for an update. Read ':=' as 'becomes' instead of 'equals', however, they are interchangeably used. As for your question, both = and == are accepted answers, as long as it is clear to your reader what your intention is.
To express equals you use the equal mark symbol once, unlike in python where you use the symbol twice to compare two values (eg if variable == 'one'). An example syntax is:
variable = 'one'
WHILE variable = 'one' DO
SEND "hi" TO DISPLAY
I saw this kind of code in vb6.
Private Enum enmMain
STEP_INIT = 1
STEP_RUN = 2
STEP_SLEEP = 3
STEP_SUSPEND = 4
STEP_ERROR = 5
End Enum
Private mStep As enmMain
Select Case mStep
Case Is <= enmMain.STEP_RUN
'Do something
Case enmMain.STEP_RUN To enmMain.STEP_ERROR
'Do something
I don't understand this:
Case enmMain.STEP_RUN To enmMain.STEP_ERROR
If it goes into that case when it meets this condition:
the latest value is STEP_RUN
current value is STEP_ERROR
How does it work?
I am posting on mobile can't write clean.
It means that the case statement will be satisfied by all values of mStep that are between 2 and 5, inclusive.
So there is an imprecision in code. Because the value STEP_RUN appears in an inclusive test twice (see the <= operator). Which behaviour is intended for STEP_RUN, the first or the second? You need to figure it out by understanding the program's logic.
Well, let's read the manual:
If testexpression matches any Case expressionlist expression, the statements following that Case clause are executed up to the next Case clause, or, for the last clause, up to End Select. Control then passes to the statement following End Select. If testexpression matches an expressionlist expression in more than one Case clause, only the statements following the first match are executed.
Select Case will run the first block that matches, and the criteria you can use to match are much more flexible than those allowed in many other languages. Case Is <= enmMain.STEP_RUN Will run for any value of mStep that is less than or equal to 2, and Case enmMain.STEP_RUN To enmMain.STEP_ERROR would run for any value between 2 and 5 inclusive.
Now it seems like somebody didn't quite understand what that meant, though, or at least wrote it in a confusing way, because for a value of 2 only the first Case would run, since as the section I quoted says only the first match is executed.
So the end result is that first 'Do something will run on values of 2 or less, and the second 'Do something will run on values of 3, 4, or 5.
I am trying to write a predicate that follows some simple conditions. It should take 2 lists and 2 variables and should return true if the first variable is located at the same position in the first list as the second variable is located in the second list. I did one part but I am struggling to get the other part working. This is what I have done so far, but in some way I need to check the rest of the positions in the list.
position([A|_],[B|_],Var1,Var2):-
A = Var1,
B = Var2.
I want for example to be able to write like this:
position([x,y,z],[1,2,3],z,3).
true .
Thanks in advance.
EDIT
I have tried and what I write keep giving me false, I thought this would work but it doesn't...:
position([A|C],[B|D],Var1,Var2):-
A = Var1,
B = Var2,
position(C,D,Var1,Var2).
Not sure why this doesn't work..
You need two clauses, one implementing the base case for when you find the variables in the same position and a clause implementing the recursive clause that you use to walk the list until either you found what you're looking for or reach the end of the lists. You're almost there:
position([Head1| _], [Head2| _], Var1, Var2) :-
Head1 == Var1,
Head2 == Var2.
position([_| Tail1], [_| Tail2], Var1, Var2) :-
position(Tail1, Tail2, Var1, Var2).
Note that you may need to use equality, (==)/2, as above instead of unification, (=)/2, as two variables always unify. But the choice depends on the possible inputs to your predicate and the exact desired semantics. Also, there can be several solutions. If you're interested in just one, you can add a cut to the end of the first clause or, in alternative, you can use a single clause with an if-then-else control construct:
position([Head1| Tail1], [Head2| Tail2], Var1, Var2) :-
( Head1 == Var1,
Head2 == Var2 ->
true
; position(Tail1, Tail2, Var1, Var2)
).
I'm working on a project. Currently I have a fairly large conditional statement, that assigns a value to a variable based on some input parameters. So, I have something like this.
if some condition
x = some value
elsif another condition
x = a different value
...
What's the best way to refactor this? I'm hoping that I might end up with something like
x = some value if some condition || another value if another condition
Is there a pattern for this sort of thing?
Just put the assignment outside the if.
x = if some condition
some value
elsif another condition
a different value
Or you could use a Hash.
x = dict[some condition]
It's not a pattern, but an operator. The one you're referring to is the ternary operator:
If Condition is true ? Then value X : Otherwise value Y
Here is an example:
speed = 90
speed > 55 ? puts("I can't drive 55!") : puts("I'm a careful driver")
Using the ternary statement is short, sweet, and does the job.
x = some condition ? some value :
another condition ? a different value : ...
A conditional statement is also an expression, so one of the first things you can do, if the variable is the same in each condition, is:
x = if cond1
expr1
elsif cond2
expr2
....
end
If the conditions are all states of a single expression, you can make this even neater, using a case statement.
However, the next most obvious re-factoring exercise is to get the big conditional isolated into a method, which should be fed the bare minimum data required to evaluate all the conditions and expressions.
E.g.
# Where conditional is currently, and x assigned, assuming the conditionals
# need a couple of variables . . .
x = foo param1, param2
# Elsewhere
private
def foo p1, p2
if cond1
expr1
elsif cond2
expr2
....
end
end
If you want to refactor for code clarity and flexibility, consider the replacing conditional with polymorphism refactor.
There's not enough detail in your question to go much further with recommendations, but this refactor will make your code base much more resistant to change. If you receive a new requirement, it's bad form to break open the conditional and modify it (more prone to introducing bugs, more difficult to do); it's preferable to create a new object that you can plug into the existing codebase. This flexibility what the Open/Closed Principle (the "O" in the SOLID acronym) describes.
Is it better to use NOT or to use <> when comparing values in VBScript?
is this:
If NOT value1 = value2 Then
or this:
If value1 <> value2 Then
better?
EDIT:
Here is my counterargument.
When looking to logically negate a Boolean value you would use the NOT operator, so this is correct:
If NOT boolValue1 Then
and when a comparison is made in the case of the first example a Boolean value is returned. either the values are equal True, or they are not False. So using the NOT operator would be appropriate, because you are logically negating a Boolean value.
For readability placing the comparison in parenthesis would probably help.
The latter (<>), because the meaning of the former isn't clear unless you have a perfect understanding of the order of operations as it applies to the Not and = operators: a subtlety which is easy to miss.
Because "not ... =" is two operations and "<>" is only one, it is faster to use "<>".Here is a quick experiment to prove it:
StartTime = Timer
For x = 1 to 100000000
If 4 <> 3 Then
End if
Next
WScript.echo Timer-StartTime
StartTime = Timer
For x = 1 to 100000000
If Not (4 = 3) Then
End if
Next
WScript.echo Timer-StartTime
The results I get on my machine:
4.783203
5.552734
Agreed, code readability is very important for others, but more importantly yourself. Imagine how difficult it would be to understand the first example in comparison to the second.
If code takes more than a few seconds to read (understand), perhaps there is a better way to write it. In this case, the second way.
The second example would be the one to go with, not just for readability, but because of the fact that in the first example, If NOT value1 would return a boolean value to be compared against value2. IOW, you need to rewrite that example as
If NOT (value1 = value2)
which just makes the use of the NOT keyword pointless.