Define the predicate maxlist( List, Max) so that Max is the greatest number in the list of numbers List2019 Community Moderator ElectionHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to randomly select an item from a list?How do you split a list into evenly sized chunks?Getting the last element of a list in PythonHow to make a flat list out of list of lists?How to get the number of elements in a list in Python?How to concatenate two lists in Python?How to clone or copy a list?
Is divide-by-zero a security vulnerability?
Did Amazon pay $0 in taxes last year?
Does an unused member variable take up memory?
Do Paladin Auras of Differing Oaths Stack?
Leveling the sagging side of the home
How should I solve this integral with changing parameters?
Will expression retain the same definition if particle is changed?
How do I increase the number of TTY consoles?
Traveling to heavily polluted city, what practical measures can I take to minimize impact?
Why aren't there more Gauls like Obelix?
What is the purpose of a disclaimer like "this is not legal advice"?
LaTeX logo in tufte-book
Having the player face themselves after the mid-game
Playing a 7-string guitar song on a 6-string guitar
How is it possible to drive VGA displays at such high pixel clock frequencies?
Why is there an extra space when I type "ls" on the Desktop?
Why is it common in European airports not to display the gate for flights until around 45-90 minutes before departure, unlike other places?
Is it appropriate to ask a former professor to order a book for me through an inter-library loan?
What will happen if my luggage gets delayed?
Can't make sense of a paragraph from Lovecraft
Should we avoid writing fiction about historical events without extensive research?
Why do we say 'Pairwise Disjoint', rather than 'Disjoint'?
Giving a career talk in my old university, how prominently should I tell students my salary?
Short scifi story where reproductive organs are converted to produce "materials", pregnant protagonist is "found fit" to be a mother
Define the predicate maxlist( List, Max) so that Max is the greatest number in the list of numbers List
2019 Community Moderator ElectionHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to randomly select an item from a list?How do you split a list into evenly sized chunks?Getting the last element of a list in PythonHow to make a flat list out of list of lists?How to get the number of elements in a list in Python?How to concatenate two lists in Python?How to clone or copy a list?
Exercise 3.17 “ Prolog Programming for Artificial Intelligence” by
Ivan Btrako
I understand the max predicate, but I'm having trouble tracing the maxList one. I'm trying to write the logic in pseudocode to help me out
- Take first and second elements and compare them and get the
maximum - Take that maximum and compare it with third element in
list and if it's bigger overwrite the maximum and repeat again till
list is empty.
I don't understand the solution given however, and I tried tracing it and failed. I'm having trouble mapping the MaxRest to the Max & with the base case. Also, I don't understand why prolog doesn't give an error when unifying [X,Y|Rest] with [Y|T]. Example [1,3,[7,2]] with [3|[7,2]].
max(X,Y,X):-
X>=Y.
max(X,Y,Y):-
X<Y.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
Any help tracing would be greatly appreciated.
list prolog
New contributor
add a comment |
Exercise 3.17 “ Prolog Programming for Artificial Intelligence” by
Ivan Btrako
I understand the max predicate, but I'm having trouble tracing the maxList one. I'm trying to write the logic in pseudocode to help me out
- Take first and second elements and compare them and get the
maximum - Take that maximum and compare it with third element in
list and if it's bigger overwrite the maximum and repeat again till
list is empty.
I don't understand the solution given however, and I tried tracing it and failed. I'm having trouble mapping the MaxRest to the Max & with the base case. Also, I don't understand why prolog doesn't give an error when unifying [X,Y|Rest] with [Y|T]. Example [1,3,[7,2]] with [3|[7,2]].
max(X,Y,X):-
X>=Y.
max(X,Y,Y):-
X<Y.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
Any help tracing would be greatly appreciated.
list prolog
New contributor
add a comment |
Exercise 3.17 “ Prolog Programming for Artificial Intelligence” by
Ivan Btrako
I understand the max predicate, but I'm having trouble tracing the maxList one. I'm trying to write the logic in pseudocode to help me out
- Take first and second elements and compare them and get the
maximum - Take that maximum and compare it with third element in
list and if it's bigger overwrite the maximum and repeat again till
list is empty.
I don't understand the solution given however, and I tried tracing it and failed. I'm having trouble mapping the MaxRest to the Max & with the base case. Also, I don't understand why prolog doesn't give an error when unifying [X,Y|Rest] with [Y|T]. Example [1,3,[7,2]] with [3|[7,2]].
max(X,Y,X):-
X>=Y.
max(X,Y,Y):-
X<Y.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
Any help tracing would be greatly appreciated.
list prolog
New contributor
Exercise 3.17 “ Prolog Programming for Artificial Intelligence” by
Ivan Btrako
I understand the max predicate, but I'm having trouble tracing the maxList one. I'm trying to write the logic in pseudocode to help me out
- Take first and second elements and compare them and get the
maximum - Take that maximum and compare it with third element in
list and if it's bigger overwrite the maximum and repeat again till
list is empty.
I don't understand the solution given however, and I tried tracing it and failed. I'm having trouble mapping the MaxRest to the Max & with the base case. Also, I don't understand why prolog doesn't give an error when unifying [X,Y|Rest] with [Y|T]. Example [1,3,[7,2]] with [3|[7,2]].
max(X,Y,X):-
X>=Y.
max(X,Y,Y):-
X<Y.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
Any help tracing would be greatly appreciated.
list prolog
list prolog
New contributor
New contributor
New contributor
asked Mar 6 at 23:37
user178008user178008
324
324
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Since you understand max/3
this won't explain it.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This is a classic recursive list pattern that does the recursion before processing the item. The normal recursive list pattern with a tail-call is
process_list([H|T],R) :-
process_item(H,R),
process_list(T,R).
process_list([],R).
but here it is
process_list([H|T],R) :-
process_list(T,R),
process_item(H,R).
process_list([],R).
where the item is processed after recursing through the list.
Also this processes two items at a time, i.e.
[H1,H2|T]
because the compare needs two items.
This
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
is just taking the values off of the list and pushing them onto the stack so that when it backtracks it will have one value for the comparison, i.e. MaxRest
which is the max of all the values that were in Rest
and the current value which is X
. You might think it should be Y
, but when there is just one item in the list, the list is actually [Y|[]]
which is really [Y]
with matches with the pattern in this clause maxlist( [X], X).
.
So what does maxlist( [X], X).
do? A common base case for a list is just predicate([],R).
for returning a list, but in this case the last value alone needs to be returned as a value, so this takes X
out of the list in the first parameter and returns it as a value in the second parameter so that it becomes MaxRest
which is the max value when there is only one item in the list. When max(X, MaxRest,Max)
is executed it has a value for MaxRest
return from backtracking, and X
on the stack when deconstructing the list. The result is Max
which is returned as the third parameter in maxlist( [X,Y|Rest], Max)
which becomes the value for MaxRest
upon backtracking again.
Ask questions if that doesn't make sense.
I don't understand why Prolog doesn't give an error when unifying
[X,Y|Rest] with [Y|Rest].
Example [1,3,[7,2]] with [3|[7,2]]
Actually
?- [1,3,[7,2]] = [3|[7,2]].
false.
so while your example does give an error, it is not what is being done with
[X,Y|Rest] and [Y|Rest]
Start with the list [1,2,3,4]
and unify it with[X,Y|Rest]
?- [1,2,3,4] = [X,Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4].
Notice that [Y|Rest]
would then be [2|[3,4]]
.
?- [1,2,3,4] = [X,Y|Rest],A = [Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4],
A = [2, 3, 4].
Don't try an unify [X,Y|Rest]
with [Y|Rest]
but instead unify Rest
with Rest
and Y
with Y
, X
is stored on the stack but not passed to next level of the stackframe.
If you really want to understand list then use this to see them
?- write_term([1,2,3,4],[dotlists(true)]).
.(1,.(2,.(3,.(4,[]))))
true.
If you have Version 4 of the book see Figure 3.1 on page 61.
add a comment |
Prolog predicates define relations in terms of logic that says that the head of the clause is true if (:-
) the body is true. This helps you properly read a predicate:
max(X,Y,X):-
X>=Y.
This says that:
X
is the maximum ofX
andY
ifX >= Y
is true.
You can similarly read the clause for max(X,Y,Y)
.
Now look at:
maxlist( [X], X).
Here, we're defining maxlist
to mean the maximum value of a list. This particular clause says that:
X
is the maximum value of the list[X]
.
There are no if conditions (:-
) in this case since no other conditions are necessary to establish this rule.
Then there is the recursive clause:
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This says that:
Max
is the maximum value of list[X,Y|Rest]
ifMaxRest
is the maximum of list[Y|Rest]
andMax
is the maximum value ofX
andMaxRest
.
This recursive clause and the prior base case clause completely define maxlist
. If you read through that carefully, it should seem completely logical.
I don't understand why prolog doesn't give an error when unifying
[X,Y|Rest]
with[Y|T]
I don't understand this comment. Nowhere in your code does Prolog attempt to unify these two terms. Your example terms of [1,3,[7,2]]
and [3|[7,2]]
would not unify because the first is a list of three elements: 1, 3, and [7,2], whereas the other is a list of 3 elements: 3, 7, and 2. For two lists to be unifiable, it not only needs to be the same length, but each corresponding term in the list must be unifiable.
What would then make [X,Y|Rest]
and [Y|T]
unifiable? You can write [X,Y|Rest]
as [X|[Y|Rest]]
, which makes it easy to see that these are unifiable if the following are true:
X = Y,
[Y|Rest] = T
In other words, the lists have to have (a) at least two elements, and (b) the first two elements are the same.
add a comment |
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: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);
);
user178008 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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%2fstackoverflow.com%2fquestions%2f55033887%2fdefine-the-predicate-maxlist-list-max-so-that-max-is-the-greatest-number-in-t%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you understand max/3
this won't explain it.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This is a classic recursive list pattern that does the recursion before processing the item. The normal recursive list pattern with a tail-call is
process_list([H|T],R) :-
process_item(H,R),
process_list(T,R).
process_list([],R).
but here it is
process_list([H|T],R) :-
process_list(T,R),
process_item(H,R).
process_list([],R).
where the item is processed after recursing through the list.
Also this processes two items at a time, i.e.
[H1,H2|T]
because the compare needs two items.
This
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
is just taking the values off of the list and pushing them onto the stack so that when it backtracks it will have one value for the comparison, i.e. MaxRest
which is the max of all the values that were in Rest
and the current value which is X
. You might think it should be Y
, but when there is just one item in the list, the list is actually [Y|[]]
which is really [Y]
with matches with the pattern in this clause maxlist( [X], X).
.
So what does maxlist( [X], X).
do? A common base case for a list is just predicate([],R).
for returning a list, but in this case the last value alone needs to be returned as a value, so this takes X
out of the list in the first parameter and returns it as a value in the second parameter so that it becomes MaxRest
which is the max value when there is only one item in the list. When max(X, MaxRest,Max)
is executed it has a value for MaxRest
return from backtracking, and X
on the stack when deconstructing the list. The result is Max
which is returned as the third parameter in maxlist( [X,Y|Rest], Max)
which becomes the value for MaxRest
upon backtracking again.
Ask questions if that doesn't make sense.
I don't understand why Prolog doesn't give an error when unifying
[X,Y|Rest] with [Y|Rest].
Example [1,3,[7,2]] with [3|[7,2]]
Actually
?- [1,3,[7,2]] = [3|[7,2]].
false.
so while your example does give an error, it is not what is being done with
[X,Y|Rest] and [Y|Rest]
Start with the list [1,2,3,4]
and unify it with[X,Y|Rest]
?- [1,2,3,4] = [X,Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4].
Notice that [Y|Rest]
would then be [2|[3,4]]
.
?- [1,2,3,4] = [X,Y|Rest],A = [Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4],
A = [2, 3, 4].
Don't try an unify [X,Y|Rest]
with [Y|Rest]
but instead unify Rest
with Rest
and Y
with Y
, X
is stored on the stack but not passed to next level of the stackframe.
If you really want to understand list then use this to see them
?- write_term([1,2,3,4],[dotlists(true)]).
.(1,.(2,.(3,.(4,[]))))
true.
If you have Version 4 of the book see Figure 3.1 on page 61.
add a comment |
Since you understand max/3
this won't explain it.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This is a classic recursive list pattern that does the recursion before processing the item. The normal recursive list pattern with a tail-call is
process_list([H|T],R) :-
process_item(H,R),
process_list(T,R).
process_list([],R).
but here it is
process_list([H|T],R) :-
process_list(T,R),
process_item(H,R).
process_list([],R).
where the item is processed after recursing through the list.
Also this processes two items at a time, i.e.
[H1,H2|T]
because the compare needs two items.
This
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
is just taking the values off of the list and pushing them onto the stack so that when it backtracks it will have one value for the comparison, i.e. MaxRest
which is the max of all the values that were in Rest
and the current value which is X
. You might think it should be Y
, but when there is just one item in the list, the list is actually [Y|[]]
which is really [Y]
with matches with the pattern in this clause maxlist( [X], X).
.
So what does maxlist( [X], X).
do? A common base case for a list is just predicate([],R).
for returning a list, but in this case the last value alone needs to be returned as a value, so this takes X
out of the list in the first parameter and returns it as a value in the second parameter so that it becomes MaxRest
which is the max value when there is only one item in the list. When max(X, MaxRest,Max)
is executed it has a value for MaxRest
return from backtracking, and X
on the stack when deconstructing the list. The result is Max
which is returned as the third parameter in maxlist( [X,Y|Rest], Max)
which becomes the value for MaxRest
upon backtracking again.
Ask questions if that doesn't make sense.
I don't understand why Prolog doesn't give an error when unifying
[X,Y|Rest] with [Y|Rest].
Example [1,3,[7,2]] with [3|[7,2]]
Actually
?- [1,3,[7,2]] = [3|[7,2]].
false.
so while your example does give an error, it is not what is being done with
[X,Y|Rest] and [Y|Rest]
Start with the list [1,2,3,4]
and unify it with[X,Y|Rest]
?- [1,2,3,4] = [X,Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4].
Notice that [Y|Rest]
would then be [2|[3,4]]
.
?- [1,2,3,4] = [X,Y|Rest],A = [Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4],
A = [2, 3, 4].
Don't try an unify [X,Y|Rest]
with [Y|Rest]
but instead unify Rest
with Rest
and Y
with Y
, X
is stored on the stack but not passed to next level of the stackframe.
If you really want to understand list then use this to see them
?- write_term([1,2,3,4],[dotlists(true)]).
.(1,.(2,.(3,.(4,[]))))
true.
If you have Version 4 of the book see Figure 3.1 on page 61.
add a comment |
Since you understand max/3
this won't explain it.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This is a classic recursive list pattern that does the recursion before processing the item. The normal recursive list pattern with a tail-call is
process_list([H|T],R) :-
process_item(H,R),
process_list(T,R).
process_list([],R).
but here it is
process_list([H|T],R) :-
process_list(T,R),
process_item(H,R).
process_list([],R).
where the item is processed after recursing through the list.
Also this processes two items at a time, i.e.
[H1,H2|T]
because the compare needs two items.
This
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
is just taking the values off of the list and pushing them onto the stack so that when it backtracks it will have one value for the comparison, i.e. MaxRest
which is the max of all the values that were in Rest
and the current value which is X
. You might think it should be Y
, but when there is just one item in the list, the list is actually [Y|[]]
which is really [Y]
with matches with the pattern in this clause maxlist( [X], X).
.
So what does maxlist( [X], X).
do? A common base case for a list is just predicate([],R).
for returning a list, but in this case the last value alone needs to be returned as a value, so this takes X
out of the list in the first parameter and returns it as a value in the second parameter so that it becomes MaxRest
which is the max value when there is only one item in the list. When max(X, MaxRest,Max)
is executed it has a value for MaxRest
return from backtracking, and X
on the stack when deconstructing the list. The result is Max
which is returned as the third parameter in maxlist( [X,Y|Rest], Max)
which becomes the value for MaxRest
upon backtracking again.
Ask questions if that doesn't make sense.
I don't understand why Prolog doesn't give an error when unifying
[X,Y|Rest] with [Y|Rest].
Example [1,3,[7,2]] with [3|[7,2]]
Actually
?- [1,3,[7,2]] = [3|[7,2]].
false.
so while your example does give an error, it is not what is being done with
[X,Y|Rest] and [Y|Rest]
Start with the list [1,2,3,4]
and unify it with[X,Y|Rest]
?- [1,2,3,4] = [X,Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4].
Notice that [Y|Rest]
would then be [2|[3,4]]
.
?- [1,2,3,4] = [X,Y|Rest],A = [Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4],
A = [2, 3, 4].
Don't try an unify [X,Y|Rest]
with [Y|Rest]
but instead unify Rest
with Rest
and Y
with Y
, X
is stored on the stack but not passed to next level of the stackframe.
If you really want to understand list then use this to see them
?- write_term([1,2,3,4],[dotlists(true)]).
.(1,.(2,.(3,.(4,[]))))
true.
If you have Version 4 of the book see Figure 3.1 on page 61.
Since you understand max/3
this won't explain it.
maxlist( [X], X). %what does this line do?
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This is a classic recursive list pattern that does the recursion before processing the item. The normal recursive list pattern with a tail-call is
process_list([H|T],R) :-
process_item(H,R),
process_list(T,R).
process_list([],R).
but here it is
process_list([H|T],R) :-
process_list(T,R),
process_item(H,R).
process_list([],R).
where the item is processed after recursing through the list.
Also this processes two items at a time, i.e.
[H1,H2|T]
because the compare needs two items.
This
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
is just taking the values off of the list and pushing them onto the stack so that when it backtracks it will have one value for the comparison, i.e. MaxRest
which is the max of all the values that were in Rest
and the current value which is X
. You might think it should be Y
, but when there is just one item in the list, the list is actually [Y|[]]
which is really [Y]
with matches with the pattern in this clause maxlist( [X], X).
.
So what does maxlist( [X], X).
do? A common base case for a list is just predicate([],R).
for returning a list, but in this case the last value alone needs to be returned as a value, so this takes X
out of the list in the first parameter and returns it as a value in the second parameter so that it becomes MaxRest
which is the max value when there is only one item in the list. When max(X, MaxRest,Max)
is executed it has a value for MaxRest
return from backtracking, and X
on the stack when deconstructing the list. The result is Max
which is returned as the third parameter in maxlist( [X,Y|Rest], Max)
which becomes the value for MaxRest
upon backtracking again.
Ask questions if that doesn't make sense.
I don't understand why Prolog doesn't give an error when unifying
[X,Y|Rest] with [Y|Rest].
Example [1,3,[7,2]] with [3|[7,2]]
Actually
?- [1,3,[7,2]] = [3|[7,2]].
false.
so while your example does give an error, it is not what is being done with
[X,Y|Rest] and [Y|Rest]
Start with the list [1,2,3,4]
and unify it with[X,Y|Rest]
?- [1,2,3,4] = [X,Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4].
Notice that [Y|Rest]
would then be [2|[3,4]]
.
?- [1,2,3,4] = [X,Y|Rest],A = [Y|Rest].
X = 1,
Y = 2,
Rest = [3, 4],
A = [2, 3, 4].
Don't try an unify [X,Y|Rest]
with [Y|Rest]
but instead unify Rest
with Rest
and Y
with Y
, X
is stored on the stack but not passed to next level of the stackframe.
If you really want to understand list then use this to see them
?- write_term([1,2,3,4],[dotlists(true)]).
.(1,.(2,.(3,.(4,[]))))
true.
If you have Version 4 of the book see Figure 3.1 on page 61.
edited 2 days ago
answered Mar 7 at 0:45
Guy CoderGuy Coder
15.9k44187
15.9k44187
add a comment |
add a comment |
Prolog predicates define relations in terms of logic that says that the head of the clause is true if (:-
) the body is true. This helps you properly read a predicate:
max(X,Y,X):-
X>=Y.
This says that:
X
is the maximum ofX
andY
ifX >= Y
is true.
You can similarly read the clause for max(X,Y,Y)
.
Now look at:
maxlist( [X], X).
Here, we're defining maxlist
to mean the maximum value of a list. This particular clause says that:
X
is the maximum value of the list[X]
.
There are no if conditions (:-
) in this case since no other conditions are necessary to establish this rule.
Then there is the recursive clause:
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This says that:
Max
is the maximum value of list[X,Y|Rest]
ifMaxRest
is the maximum of list[Y|Rest]
andMax
is the maximum value ofX
andMaxRest
.
This recursive clause and the prior base case clause completely define maxlist
. If you read through that carefully, it should seem completely logical.
I don't understand why prolog doesn't give an error when unifying
[X,Y|Rest]
with[Y|T]
I don't understand this comment. Nowhere in your code does Prolog attempt to unify these two terms. Your example terms of [1,3,[7,2]]
and [3|[7,2]]
would not unify because the first is a list of three elements: 1, 3, and [7,2], whereas the other is a list of 3 elements: 3, 7, and 2. For two lists to be unifiable, it not only needs to be the same length, but each corresponding term in the list must be unifiable.
What would then make [X,Y|Rest]
and [Y|T]
unifiable? You can write [X,Y|Rest]
as [X|[Y|Rest]]
, which makes it easy to see that these are unifiable if the following are true:
X = Y,
[Y|Rest] = T
In other words, the lists have to have (a) at least two elements, and (b) the first two elements are the same.
add a comment |
Prolog predicates define relations in terms of logic that says that the head of the clause is true if (:-
) the body is true. This helps you properly read a predicate:
max(X,Y,X):-
X>=Y.
This says that:
X
is the maximum ofX
andY
ifX >= Y
is true.
You can similarly read the clause for max(X,Y,Y)
.
Now look at:
maxlist( [X], X).
Here, we're defining maxlist
to mean the maximum value of a list. This particular clause says that:
X
is the maximum value of the list[X]
.
There are no if conditions (:-
) in this case since no other conditions are necessary to establish this rule.
Then there is the recursive clause:
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This says that:
Max
is the maximum value of list[X,Y|Rest]
ifMaxRest
is the maximum of list[Y|Rest]
andMax
is the maximum value ofX
andMaxRest
.
This recursive clause and the prior base case clause completely define maxlist
. If you read through that carefully, it should seem completely logical.
I don't understand why prolog doesn't give an error when unifying
[X,Y|Rest]
with[Y|T]
I don't understand this comment. Nowhere in your code does Prolog attempt to unify these two terms. Your example terms of [1,3,[7,2]]
and [3|[7,2]]
would not unify because the first is a list of three elements: 1, 3, and [7,2], whereas the other is a list of 3 elements: 3, 7, and 2. For two lists to be unifiable, it not only needs to be the same length, but each corresponding term in the list must be unifiable.
What would then make [X,Y|Rest]
and [Y|T]
unifiable? You can write [X,Y|Rest]
as [X|[Y|Rest]]
, which makes it easy to see that these are unifiable if the following are true:
X = Y,
[Y|Rest] = T
In other words, the lists have to have (a) at least two elements, and (b) the first two elements are the same.
add a comment |
Prolog predicates define relations in terms of logic that says that the head of the clause is true if (:-
) the body is true. This helps you properly read a predicate:
max(X,Y,X):-
X>=Y.
This says that:
X
is the maximum ofX
andY
ifX >= Y
is true.
You can similarly read the clause for max(X,Y,Y)
.
Now look at:
maxlist( [X], X).
Here, we're defining maxlist
to mean the maximum value of a list. This particular clause says that:
X
is the maximum value of the list[X]
.
There are no if conditions (:-
) in this case since no other conditions are necessary to establish this rule.
Then there is the recursive clause:
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This says that:
Max
is the maximum value of list[X,Y|Rest]
ifMaxRest
is the maximum of list[Y|Rest]
andMax
is the maximum value ofX
andMaxRest
.
This recursive clause and the prior base case clause completely define maxlist
. If you read through that carefully, it should seem completely logical.
I don't understand why prolog doesn't give an error when unifying
[X,Y|Rest]
with[Y|T]
I don't understand this comment. Nowhere in your code does Prolog attempt to unify these two terms. Your example terms of [1,3,[7,2]]
and [3|[7,2]]
would not unify because the first is a list of three elements: 1, 3, and [7,2], whereas the other is a list of 3 elements: 3, 7, and 2. For two lists to be unifiable, it not only needs to be the same length, but each corresponding term in the list must be unifiable.
What would then make [X,Y|Rest]
and [Y|T]
unifiable? You can write [X,Y|Rest]
as [X|[Y|Rest]]
, which makes it easy to see that these are unifiable if the following are true:
X = Y,
[Y|Rest] = T
In other words, the lists have to have (a) at least two elements, and (b) the first two elements are the same.
Prolog predicates define relations in terms of logic that says that the head of the clause is true if (:-
) the body is true. This helps you properly read a predicate:
max(X,Y,X):-
X>=Y.
This says that:
X
is the maximum ofX
andY
ifX >= Y
is true.
You can similarly read the clause for max(X,Y,Y)
.
Now look at:
maxlist( [X], X).
Here, we're defining maxlist
to mean the maximum value of a list. This particular clause says that:
X
is the maximum value of the list[X]
.
There are no if conditions (:-
) in this case since no other conditions are necessary to establish this rule.
Then there is the recursive clause:
maxlist( [X,Y|Rest], Max):-
maxlist( [Y|Rest], MaxRest),
max(X, MaxRest,Max).
This says that:
Max
is the maximum value of list[X,Y|Rest]
ifMaxRest
is the maximum of list[Y|Rest]
andMax
is the maximum value ofX
andMaxRest
.
This recursive clause and the prior base case clause completely define maxlist
. If you read through that carefully, it should seem completely logical.
I don't understand why prolog doesn't give an error when unifying
[X,Y|Rest]
with[Y|T]
I don't understand this comment. Nowhere in your code does Prolog attempt to unify these two terms. Your example terms of [1,3,[7,2]]
and [3|[7,2]]
would not unify because the first is a list of three elements: 1, 3, and [7,2], whereas the other is a list of 3 elements: 3, 7, and 2. For two lists to be unifiable, it not only needs to be the same length, but each corresponding term in the list must be unifiable.
What would then make [X,Y|Rest]
and [Y|T]
unifiable? You can write [X,Y|Rest]
as [X|[Y|Rest]]
, which makes it easy to see that these are unifiable if the following are true:
X = Y,
[Y|Rest] = T
In other words, the lists have to have (a) at least two elements, and (b) the first two elements are the same.
edited 2 days ago
answered Mar 7 at 1:30
lurkerlurker
44.9k74574
44.9k74574
add a comment |
add a comment |
user178008 is a new contributor. Be nice, and check out our Code of Conduct.
user178008 is a new contributor. Be nice, and check out our Code of Conduct.
user178008 is a new contributor. Be nice, and check out our Code of Conduct.
user178008 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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%2fstackoverflow.com%2fquestions%2f55033887%2fdefine-the-predicate-maxlist-list-max-so-that-max-is-the-greatest-number-in-t%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');
);
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');
);
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');
);
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