Basic logic problem in Prolog Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Bibtex reader using PrologSample n items without replacement in PrologImplementing a zipwithindex predicate in prolog“Hello world!” in Prologcat program in PrologMonkey-banana problem in PrologLogic Prolog compilerFamily tree in PrologZebra puzzle in PrologEcho in prolog with single print
How would a mousetrap for use in space work?
Why are my pictures showing a dark band on one edge?
Significance of Cersei's obsession with elephants?
Can I infer the range of a random variable based on a confidence interval for the mean?
What is an "asse" in Elizabethan English?
Customizing QGIS plugins
C's equality operator on converted pointers
Is it fair for a professor to grade us on the possession of past papers?
How could we fake a moon landing now?
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
How do I find out the mythology and history of my Fortress?
Concentration's Meaning
Did any compiler fully use 80-bit floating point?
How much damage would a cupful of neutron star matter do to the Earth?
What is Adi Shankara referring to when he says "He has Vajra marks on his feet"?
The Nth Gryphon Number
Google .dev domain strangely redirects to https
Is CEO the "profession" with the most psychopaths?
Why are vacuum tubes still used in amateur radios?
Misunderstanding of Sylow theory
What is the home of the drow in Flanaess?
Random body shuffle every night—can we still function?
How many time has Arya actually used Needle?
Is there public access to the Meteor Crater in Arizona?
Basic logic problem in Prolog
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Bibtex reader using PrologSample n items without replacement in PrologImplementing a zipwithindex predicate in prolog“Hello world!” in Prologcat program in PrologMonkey-banana problem in PrologLogic Prolog compilerFamily tree in PrologZebra puzzle in PrologEcho in prolog with single print
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I thought it would be a good idea to possible take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog:
A messy kid wrote a multiplication problem.
- Alice saw 100 x 6.
- Bob saw 101 x 6.
- Dan saw 102 x 9.
Each one only misread one digit. What is the real solution to the problem?
It proved to be (much) trickier to model than solve by hand, but here is what I came up with:
%- Read person saw number at position.
saw(alice, 1, 0).
saw(alice, 0, 1).
saw(alice, 0, 2).
saw(alice, 6, 3).
saw(bob, 1, 0).
saw(bob, 0, 1).
saw(bob, 1, 2).
saw(bob, 6, 3).
saw(dan, 1, 0).
saw(dan, 0, 1).
saw(dan, 2, 2).
saw(dan, 9, 3).
%- Consider the case when two people see one number and one person saw a anoth-
% er number. This doesnt actually mean the person "definitely" misread the nu-
% mber, but if the problem can be solved it measns they definitely did.
definitely_misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
saw(Q, D, Position), Q == Person, D == Digit,
saw(R, D, Position), R == Q, R == Person.
%- Read a person misread the digit at poisition at position.
misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
not((definitely_misread(Person, D, P), D == Digit, P == Position)),
(saw(Q, D1, Position), Q == Person, D1 == Digit),
(saw(R, D2, Position), R == Q, R == Person, D2 == Digit).
%- Resolve if the question is actually the correct digit at that position.
correct(Digit, Position) :-
(saw(alice, Digit, Position), not(misread(alice, Digit, Position)));
(saw(bob, Digit, Position), not(misread(bob, Digit, Position)));
(saw(dan, Digit, Position), not(misread(dan, Digit, Position))).
And thus one can get the correct solutions by calling correct (although, it displays some digit position pairings multiple times):
?- correct(D, P).
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = P, P = 2 ;
false.
prolog
$endgroup$
add a comment |
$begingroup$
I thought it would be a good idea to possible take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog:
A messy kid wrote a multiplication problem.
- Alice saw 100 x 6.
- Bob saw 101 x 6.
- Dan saw 102 x 9.
Each one only misread one digit. What is the real solution to the problem?
It proved to be (much) trickier to model than solve by hand, but here is what I came up with:
%- Read person saw number at position.
saw(alice, 1, 0).
saw(alice, 0, 1).
saw(alice, 0, 2).
saw(alice, 6, 3).
saw(bob, 1, 0).
saw(bob, 0, 1).
saw(bob, 1, 2).
saw(bob, 6, 3).
saw(dan, 1, 0).
saw(dan, 0, 1).
saw(dan, 2, 2).
saw(dan, 9, 3).
%- Consider the case when two people see one number and one person saw a anoth-
% er number. This doesnt actually mean the person "definitely" misread the nu-
% mber, but if the problem can be solved it measns they definitely did.
definitely_misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
saw(Q, D, Position), Q == Person, D == Digit,
saw(R, D, Position), R == Q, R == Person.
%- Read a person misread the digit at poisition at position.
misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
not((definitely_misread(Person, D, P), D == Digit, P == Position)),
(saw(Q, D1, Position), Q == Person, D1 == Digit),
(saw(R, D2, Position), R == Q, R == Person, D2 == Digit).
%- Resolve if the question is actually the correct digit at that position.
correct(Digit, Position) :-
(saw(alice, Digit, Position), not(misread(alice, Digit, Position)));
(saw(bob, Digit, Position), not(misread(bob, Digit, Position)));
(saw(dan, Digit, Position), not(misread(dan, Digit, Position))).
And thus one can get the correct solutions by calling correct (although, it displays some digit position pairings multiple times):
?- correct(D, P).
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = P, P = 2 ;
false.
prolog
$endgroup$
add a comment |
$begingroup$
I thought it would be a good idea to possible take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog:
A messy kid wrote a multiplication problem.
- Alice saw 100 x 6.
- Bob saw 101 x 6.
- Dan saw 102 x 9.
Each one only misread one digit. What is the real solution to the problem?
It proved to be (much) trickier to model than solve by hand, but here is what I came up with:
%- Read person saw number at position.
saw(alice, 1, 0).
saw(alice, 0, 1).
saw(alice, 0, 2).
saw(alice, 6, 3).
saw(bob, 1, 0).
saw(bob, 0, 1).
saw(bob, 1, 2).
saw(bob, 6, 3).
saw(dan, 1, 0).
saw(dan, 0, 1).
saw(dan, 2, 2).
saw(dan, 9, 3).
%- Consider the case when two people see one number and one person saw a anoth-
% er number. This doesnt actually mean the person "definitely" misread the nu-
% mber, but if the problem can be solved it measns they definitely did.
definitely_misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
saw(Q, D, Position), Q == Person, D == Digit,
saw(R, D, Position), R == Q, R == Person.
%- Read a person misread the digit at poisition at position.
misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
not((definitely_misread(Person, D, P), D == Digit, P == Position)),
(saw(Q, D1, Position), Q == Person, D1 == Digit),
(saw(R, D2, Position), R == Q, R == Person, D2 == Digit).
%- Resolve if the question is actually the correct digit at that position.
correct(Digit, Position) :-
(saw(alice, Digit, Position), not(misread(alice, Digit, Position)));
(saw(bob, Digit, Position), not(misread(bob, Digit, Position)));
(saw(dan, Digit, Position), not(misread(dan, Digit, Position))).
And thus one can get the correct solutions by calling correct (although, it displays some digit position pairings multiple times):
?- correct(D, P).
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = P, P = 2 ;
false.
prolog
$endgroup$
I thought it would be a good idea to possible take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog:
A messy kid wrote a multiplication problem.
- Alice saw 100 x 6.
- Bob saw 101 x 6.
- Dan saw 102 x 9.
Each one only misread one digit. What is the real solution to the problem?
It proved to be (much) trickier to model than solve by hand, but here is what I came up with:
%- Read person saw number at position.
saw(alice, 1, 0).
saw(alice, 0, 1).
saw(alice, 0, 2).
saw(alice, 6, 3).
saw(bob, 1, 0).
saw(bob, 0, 1).
saw(bob, 1, 2).
saw(bob, 6, 3).
saw(dan, 1, 0).
saw(dan, 0, 1).
saw(dan, 2, 2).
saw(dan, 9, 3).
%- Consider the case when two people see one number and one person saw a anoth-
% er number. This doesnt actually mean the person "definitely" misread the nu-
% mber, but if the problem can be solved it measns they definitely did.
definitely_misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
saw(Q, D, Position), Q == Person, D == Digit,
saw(R, D, Position), R == Q, R == Person.
%- Read a person misread the digit at poisition at position.
misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
not((definitely_misread(Person, D, P), D == Digit, P == Position)),
(saw(Q, D1, Position), Q == Person, D1 == Digit),
(saw(R, D2, Position), R == Q, R == Person, D2 == Digit).
%- Resolve if the question is actually the correct digit at that position.
correct(Digit, Position) :-
(saw(alice, Digit, Position), not(misread(alice, Digit, Position)));
(saw(bob, Digit, Position), not(misread(bob, Digit, Position)));
(saw(dan, Digit, Position), not(misread(dan, Digit, Position))).
And thus one can get the correct solutions by calling correct (although, it displays some digit position pairings multiple times):
?- correct(D, P).
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = P, P = 2 ;
false.
prolog
prolog
asked 4 mins ago
DairDair
4,6591932
4,6591932
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217755%2fbasic-logic-problem-in-prolog%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217755%2fbasic-logic-problem-in-prolog%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown