I am solving a test for an exam and I got this question with the following answers:
fail
yes
true
no
Running it in the shell i get True, but i cannot understand how this answer is obtained.
I get the first part true, fail;but the rest is a mistery.
Am i supposed to look just at the last part true, not fail?
true, fail; not true, not fail; true, not fail
Can be translated into the following logical expression:
(true and false) or (((not true) and (not false)) or (true and (not false)))
That we can simplify progressively...
(true and false) or ((false and true) or (true and true))
false or (false or true)
false or true
true
Related
When I run the following snippet, and enter an acceptable value, I get the desired result.
do while len(strselect) = 0 'or strselect<>"1" or strselect<>"2" or strselect<>"3"
strselect = inputbox ("Please select:" &vbcrlf&vbcrlf&_
"1. Add an entry" &vbcrlf&vbcrlf&_
"2. Remove an entry" &vbcrlf&vbcrlf&_
"3. Search for an entry" &vbcrlf, "Contact Book")
if isempty(strselect) then
wscript.quit()
elseif strselect="1" then
wscript.echo "You chose 1"
elseif strselect="2" then
wscript.echo "You chose 2"
elseif strselect="3" then
wscript.echo "You chose 3"
end if
loop
However if I try constrain the validation process further (by including the remark in the do while conditions), and run the snippet again, I get the corresponding if condition triggered, but the do loop continues, instead of exiting.
I've tried using isnumeric and cstr on the do loop strselect conditions, with no joy... What am I missing to get the darn thing to exit the loop?
You have a problem with the logic in the condition
condition 1 condition 2 condition 3 condition 4
v----------------v v------------v v------------v v............v
do while len(strselect) = 0 or strselect<>"1" or strselect<>"2" or strselect<>"3"
Depending on value inside strselect, you have
value c1 c2 c3 c4
len=0 <>"1" <>"2" <>"3" c1 or c2 or c3 or c4
-------------------------------------- --------------------
empty true true true true true
1 false false true true true
2 false true false true true
3 false true true false true
other false true true true true
In each line you have at least one condition evaluated as true, and as you are concatenating the conditions with Or operators (evaluate to true if at least one of the values is true), the full condition is evaluated as true and the code keeps looping
You only need to change the condition
Do While strselect<>"1" And strselect<>"2" And strselect<>"3"
Do While Not (strselect="1" Or strselect="2" Or strselect="3")
....
regions([a,b,c,d,e,f]).
colors([brown,green,blue,red]).
hascolor(X,brown):-regions([X|_]).
hascolor(X,brown):-regions([_,_,_,_,X,_]).
hascolor(X,blue):-regions([_,X,_,_,_,_]).
hascolor(X,blue):-regions([_,_,_,X,_,_]).
hascolor(X,green):-regions([_,_,_,_,_,X]).
hascolor(X,red):-regions([_,_,X,_,_,_]).
conflict(X,Y):-hascolor(X,brown),hascolor(Y,brown).
Hey guys running ?-conflict(a,e). would get me true and false. Both a and e has the same color(brown) and conflict should return true only. Why does it return false also?
Hey I am wondering if there is a way to collapse this bit of code, so its not 12 lines of setting variables.
Current Code:
set isVideo to false
set isTV to false
set isMovie to false
set isRawVideo to false
set isDocumentary to false
set isAudio to false
set isSports to false
set isUnknown to false
set toPrompt to false
set BTNChoice to ""
set keyword to ""
set keywordHit to ""
What I'm hoping for:
set (isVideo, isTV, isMovie, isRawVideo, isDocumentary, isAudio, isSports, isUnknown, toPrompt) to false
set (BTNChoice, keyword, keywordHit) to ""
Or:
isVideo = isTV = isMovie = isRawVideo = isDocumentary = isAudio = isSports = isUnknown = toPrompt = false
BTNChoice = keyword = keywordHit = ""
Let me know if theres a way to reduce this or if I'm stuck with one variable per line.
Ryan
The only one-liner way is
set {isVideo, isTV, isMovie, isRawVideo, isDocumentary, isAudio, isSports, isUnknown, toPrompt} to {false, false, false, false, false, false, false, false, false}
set {BTNChoice, keyword, keywordHit} to {"", "", ""}
Or even a real one-liner
set {isVideo, isTV, isMovie, isRawVideo, isDocumentary, isAudio, isSports, isUnknown, toPrompt, BTNChoice, keyword, keywordHit} to {false, false, false, false, false, false, false, false, false, "", "", ""}
The number of items on the right side must not be less than the number of items on the left side.
I'm trying to write a critical section guarded by a mutex in SWI-Prolog and have been looking at using setup_call_cleanup/3 and setup_call_catcher_cleanup/4.
The problem I have is that my Goal is a sequence of operations of which any may fail and that means the system backtracks to the start of setup_call_cleanup and calls Cleanup. Unfortunately, with backtracking I'm not able to report the error appropriately. To illustrate my issue let's consider this simple example:
setup_call_cleanup(
mutex_lock(mtx),
( Step1 = true, Step2 = true, Step3 = true ),
( mutex_unlock(mtx), writeln([Step1, Step2, Step3]) ).
and compare it with the following:
setup_call_cleanup(
mutex_lock(mtx),
( Step1 = true, Step2 = true, fail, Step3 = true ),
( mutex_unlock(mtx), writeln([Step1, Step2, Step3]) ).
In the first case all is ok -- I can see all steps done. But in the second case I'm not able to see that Step1 and Step2 has been carried out. I'd like to see it because they may have external side effects which backtracking cannot undo. Also, I don't want to include error handling within the Goal to make the critical section as lean and fast as possible.
I have two ideas:
Decorate each step with nb_setval to store a value to indicate the completed steps,
Re-code steps, so they throw exceptions that carry details of the problem.
The former will make code rather bloated, whereas the latter seems too heavyweight for my needs. Is there anything like setup_nb_call_cleanup?
The trick, I think, is to run the goals one by one, guarded for errors and failure and return step that failed. A good start is
until_failure((A,B), Result) :-
!,
until_failure(A, Result),
( var(Result)
-> until_failure(B, Result)
; true
).
until_failure(G, Result) :-
( catch(G, Result, true)
*-> true
; Result = false(G)
).
Now you can run e.g.,
?- until_failure((Step1 = true,
Step2 = true,
fail,
Step3 = true), Result),
writeln([Step1, Step2, Step3]).
[true, true, _5742]
Result = false(fail)
See http://swish.swi-prolog.org/p/ReQWsvCg.swinb. SWISH doesn't allow for
handling mutexes, but you can easily wrap this inside with_mutex/2. The details depend notably on how you want to handle non-determinism.
Thank you Jan for the inspiration; very useful. I ended up coding a similar step_by_step rule:
step_by_step(Goal, Steps, Error) :-
step_by_step_(Goal, 0, Steps, Error).
step_by_step_((A, B), InStep, OutStep, Error) :-
!,
step_by_step_(A, InStep, OutStep1, Error),
( var(Error) ->
step_by_step_(B, OutStep1, OutStep, Error)
;
OutStep = InStep
).
step_by_step_(Goal, InStep, OutStep, Error) :-
( catch(Goal, Ex, (Error = exception(Ex), OutStep = InStep)) *->
(OutStep is InStep + 1 ; true), !
;
Error = false(Goal),
OutStep = InStep
).
I'm not happy with (OutStep is InStep + 1 ; true), ! but wasn't able to find a better way.
Anyway, the rule gives me what I want:
-- if all goes ok, it just runs all steps in sequence:
?- step_by_step((Step1 = true, Step2 = true, Step3 = true), Steps, Error).
Step1 = Step2, Step2 = Step3, Step3 = true,
Steps = 3.
-- if one of the step fails or throws an exception, it returns the number of steps completed successfully and the failed goal:
?- step_by_step((Step1 = true, Step2 = true, fail, Step3 = true), Steps, Error).
Step1 = Step2, Step2 = true,
Steps = 2,
Error = false(fail).
or the exception:
?- step_by_step((Step1 = true, Step2 = true, throw(bomb), Step3 = true), Steps, Error).
Step1 = Step2, Step2 = true,
Steps = 2,
Error = exception(bomb).
I'm working with a bit of old VB6 code that goes thus...
Dim STATUS As Integer
STATUS = -1
If (Not STATUS) Then
' do something
Else
' do something else
End If
so I was, naturally, wondering which branch of this code is executed. So does anyone know what the numeric values of True and False are in VB6?
True is stored as -1 and false as 0. Any non-zero value is considered as true.
To see why it is so please check - http://www.vbforums.com/showthread.php?t=405047
In VB 6, True has a numeric value of -1. False has a numeric value of 0.
The reason for this is because the Boolean data type is stored as a 16-bit signed integer. Therefore,-1 evaluates to 16 1s in binary (1111111111111111). False is 16 0s (0000000000000000). This produces the relationship that has held throughout the evolution of BASIC: True = Not False.
Not really an answer, but just poking about, I typed this into the immediate window, with these results:
For x = -5 To 5 : ? x, CBool(x), ( x = True ), ( x = False ) : Next x
-5 True False False
-4 True False False
-3 True False False
-2 True False False
-1 True True False
0 False False True
1 True False False
2 True False False
3 True False False
4 True False False
5 True False False
(I tested more values, but only -1 and 0 had anything "interesting" going on. The rest were all True/False/False.) So, empirically, I'd say that the comparison is being done arithmetically unless you cast with CBool. Why? I can't really say...