I'm not sure how this works, never seen such an operator "= -" - logic

I was reading someone's code and found out that he wrote the code (below following).
for example:
operandA = -operandA;
That works but I don't understand how this operator is working. Can anyone give any similar idea please?
In the program's logic, the given statement changes the movement of X (operandA) to reverse direction when it hits something.

I believe it is just setting operandA to the negative of operandA

Related

Error when setting a variable

This is probably a really easy error but I've spent the last 2 hours on it and I just cant get anywhere with it. My code produces sentence(np(det(a), np2(adj(very), np2(adj(old), np2(noun(woman))))), vp(verb(loves), np(det(a), np2(adj(tricky), np2(noun(crossword))) and I'm starting to split it by the use of
split(sentence(np(det(_), np2(A)), VP)) :-
Write(A).
for some reason it fails but cant for the life of me find out why. Sorry for the novice question.
You are trying to match np2(A) with np2(adj(...),np2(...))
Try
split(sentence(np(det(_), np2(A,_)), VP)) :-
write(A).
Sorry for the novice answer but I only studied a very minimal amount of bprolog in my college days.
I just counted your brackets and there appears to be three missing closing braces at the very end to close the np2(adj, vp( and the sentence(.

Depth First Search Prolog

I'm trying to solve a water, jug problem (one 7L, one 4L, get 5L in the 7L jug) using dept first search. However something keeps going wrong whenever I try to get a new state back from one of my actions.
Prolog Code
I can't figure out what is going wrong, this is what the output looks like after trace:
enter image description here
Thanks in advance for any help!
You should copy and paste your code into your question; we cannot copy and paste it from your images, which makes it more work to help you, which in turn makes it less likely that we will help.
Some problems I noticed anyway:
Your first rule for go_to_goal/3 does not talk about the relation between ClosedList and Path. You will compute the path but will never be able to communicate it to the caller. (Then again, you also ignore Path in solve/0...) If your Prolog system gives you "singleton variable" warnings, you should never ignore them!
You are using the == operator wrong. The goal State == (5, X) states that at the end you are looking for a pair where the first component is 5 (this part is fine) and the second component is an unbound variable. In fact, after your computations, the second component of the pair will be bound to some arithmetic term. This comparison will always fail. You should use the = (unification) operator instead. == is only used rarely, in particular situations.
If you put a term like X+Y-7 into the head of a rule, it will not be evaluated to a number. If you want it to be evaluated to a number, you must use is/2 in the body of your rules.
Your most immediate problem, however, is the following (visible from the trace you posted): The second clause of go_to_goal/3 tries to call action/2 with a pair (0, 0) as the first argument. This always fails because the first argument of every clause of action/2 is a term state(X, Y). If you change this to state(0, 0) in go_to_goal/3, you should be able to make a little bit of progress.

"Translating" a strange pseudocode into python

I'm doing an assignment and in one of the questions, my professor put some strange pseudo code as a condition and frankly I'm not sure if I understood it correctly.
This is what he gave us:
LOOP if S>0 then {S:=S-1; exit} end_if;
END_LOOP
can I understand this as
while True:
if S>0:
S = S - 1
break
if I were to rewrite it in Python?
Or, should it be like this?
while S>0:
S = S -1
break
It's not 100% clear, but given that the first version will either go around once, or go around forever, it's likely to be the second one.

Why won't finding polynomial whole number routes work?

So I want this to find me the roots of a polynomial. However, everytime I run it, it never gives me any roots, even if I use an obvious one like 2x-2. Why won't it work?
Input "Degree?",θ
Disp "Left to right"
Disp "coefficients"
1→V
For(Z,0,θ)
Input A
Q→R
P→Q
O→P
N→O
M→N
L→M
K→L
J→K
I→J
H→I
G→H
F→G
E→F
D→E
C→D
B→C
A→B
If V=1
Then
A→S
V=0
End
End
For(T,–A,A)
For(U,–W,W)
If T≠0
U/T→X
RX+Q→Y
YX+P→Z
ZX+O→Y
YX+N→Z
ZX+M→Y
YX+L→Z
ZX+K→Y
YX+J→Z
ZX+I→Y
YX+H→Z
ZX+G→Y
YX+F→Z
ZX+E→Y
YX+D→Z
ZX+C→Y
YX+B→Z
If Z=0
Then
Disp X
End
End
End
prgmRESET
RESET just resets the variable values. What is wrong with it?
Request: I have absolutely no idea what operation you are working off of, if you could please state that
Observation: You're using a lot of variables that haven't had any value assigned to them or initially cleared, I can see that you're trying to create a 'stream' of variables to work with, but if you do this without clearing the variables ahead of time then you create problems in your later calculations.
Coding Recommendations:
You state V=0, which does nothing in this context, instead of assigning it a value
You can change 'If T≠0' into just 'If T'
In your third 'For()' statement, "W" is undefined in the code.
You can change 'If Z=0:Then:Disp X:End', near the end of your code, into just 'If not(Z:Disp X'
Move prgmRESET to the top of your program
To be honest, I'm not entirely sure how you code is supposed to find the routes of a polynomial. Your error is most likely occurring somewhere in your mess of variable assigning/reassigning/swapping. I would redo your code using lists instead of basic variables.
If all you want to do is find the routes of a polynomial, I can give you a program for that.
:Prompt L1,X
:Repeat 1=dim(L1
:dim(L1->dim(L3
:seq(L1(A)(Ans-A),A,1,Ans-1->L2
:Repeat abs(Ans)<10^(-7
:L1(1->L3(1
:For(A,2,dim(L1
:XL3(A-1)+L1(A->L3(A
:End
:Ans->B
:L2(1->L3(1
:For(A,2,dim(L2
:XL3(A-1)+L2(A->L3(A
:End
:Ans^-1(AnsX-B->X
:B
:End
:Disp X
:L1(1->L2(1
:For(A,2,dim(L1)-1
:XL2(A-1)+L1(A->L2(A
:End
:L2->L1
:End
I'm not quite sure what you're trying to do here. You use a whole lot of variables without ever clearing or defining them, which probably means that all of your values will be 0.
Also, recommendation for future TI-BASIC questions:
PLEASE explain your variables. There's nothing worse than having a mess of variables and expecting the reader to do detective work to find out what they're supposed to do. Plus, it's helpful for you as well when you decide to come back to it for troubleshooting.

Flowpattern doesn't exist

I have been working on a code in prolog for a while now and it is near compiling worthy and all my ideas seem to be solid so it should work when it compiles. It is a program that consults a database file for a list of clauses and then it awaits for a query by the user which it will then pick what information it needs from the sentence and query the database appropriately but there is a block of code that keeps giving me errors complaining that the flowpattern doesn't exist in the standard predicate this may be a silly question but even with all the looking into this I have done i can't find out how to fix this problem if someone could help me out or point me in the right direction that would be greatly appreciated.
Here is the block of code that gives the error:
loop(STR):-
scan(STR,LIST),
filter(LIST,LISroT1),
pars(LIST1,LIST2),
fail.
loop(STR):- STR >< "",readquery(L),loop(L).
readquery(QUERY):-nl,nl,write("Query: "),readln(QUERY).
scan(STR,[TOK|LIST]):-
fronttoken(STR,SYMB,STR1),!,
upper_lower(SYMB,TOK),
scan(STR1,LIST).
the specific line that the compiler complains about is fronttoken(STR,SYMB,STR),!,
any help will be apreaciated thanks!
Since we are looking at an "ex[c]er[p]t" of the code, it's hard to be sure what is going wrong, but the the given evidence points to this: loop/1 is being called before readquery/1 can do its work to populate (bind) the argument STR to loop/1.
Notice that loop/1 calls itself (recursively), and does so in a repeat/fail pattern. But the first time loop/1 runs, there's no indication in the code shown of how argument STR would get populated.
A clearer (more self-contained) code snippet would be like this:
loop :-
readquery(STR),
scan(STR,LIST),
filter(LIST,LISroT1),
pars(LIST1,LIST2),
fail.
loop :- loop.
This makes it clear that predicate loop doesn't actually return any result (and the given code snippet isn't complete enough to make clear what the program as a whole accomplishes). It assumes that the clauses ahead of fail in loop are deterministic, so that in failing, control passes through to the second (recursive) clause of loop/0. If this is not the case, the determinism could be forced by wrapping each call inside once/1.

Resources