Author |
Message |
Vincen #1 / 6
|
 2 simple programs
Hi everybody, I'm new user of prolog and I meet problems with doing 2 things : 1/ I want to modify all the occurences of the 'a' character by 'b' in a list. I do this but it returns "no" : modify([], []). modify([a|List], [b|Result]) :- modify(X, Result). modify([Z|Y], [Z|Result]) :- modify(Y, Result). What's wrong??? 2/ I want to find the maximum of an integer list : I try this one : maxList([X], X). maxList([X|List], Y) :- maxList(List, Z), max(X, Y, Z). {max is a function wich put in Z the maximum between X and Y}. In fact, I don't understand why this one work : maxList([X], X). maxList([X, Y|List], Result) :- maxList([Y|List], Maxi), max(X, Maxi, Result). I hope you can help me! (mainly on the 1/ ) Thanks. -- Vincent
|
Sat, 22 May 2004 01:47:28 GMT |
|
 |
Martin Sondergaar #2 / 6
|
 2 simple programs
Quote: > Hi everybody, > I'm new user of prolog and I meet problems with doing 2 things : > 1/ I want to modify all the occurences of the 'a' character by 'b' in a > list. > I do this but it returns "no" : > modify([], []). > modify([a|List], [b|Result]) :- modify(X, Result). > modify([Z|Y], [Z|Result]) :- modify(Y, Result). > What's wrong???
The second clause is wrong. You have used two different variables, "List" and "X", which should be the same. It should be : modify( [a|List], [b|Result] ) :- modify( List, Result ). I would write it like this : modify( [a|List], [b|List1] ) :- modify( List, List1 ). The other clauses look correct to me. So your answer was very nearly right. :-) You will find your programs easier to read if you align them in the usual way, instead of having a clause all on one line. -- Martin Sondergaard, London.
|
Sat, 22 May 2004 03:13:17 GMT |
|
 |
Vincent Peytavi #3 / 6
|
 2 simple programs
Quote: > > 1/ I want to modify all the occurences of the 'a' character by 'b' in a > > list. > > I do this but it returns "no" : > > modify([], []). > > modify([a|List], [b|Result]) :- modify(X, Result). > > modify([Z|Y], [Z|Result]) :- modify(Y, Result). > > What's wrong??? > The second clause is wrong. > You have used two different variables, "List" and "X", > which should be the same.
Yes! This error occurs when I copied this clause with my keyboard. Quote: > -- > Martin Sondergaard, > London.
-- Vincent, Grenoble - France FAQ de fr.comp.lang.java http://www.faqs.org/faqs/fr/comp/lang/faq-java/
|
Sat, 22 May 2004 05:38:55 GMT |
|
 |
Esp #4 / 6
|
 2 simple programs
Answer for 2/ Quote: > maxList([X|List], Y) :- maxList(List, Z), max(X, Y, Z).
means : X is the first, Z is the max of List, Z is the max between X and Y ??? maxList([X|List], Z) :- maxList(List, Y), max(X, Y, Z). means : X is the first, Y is the max of List, Z is the max between X and Y - right and it should work (I can't test it) hth Esp
Quote: > Hi everybody, > I'm new user of prolog and I meet problems with doing 2 things : > 1/ I want to modify all the occurences of the 'a' character by 'b' in a > list. > I do this but it returns "no" : > modify([], []). > modify([a|List], [b|Result]) :- modify(X, Result). > modify([Z|Y], [Z|Result]) :- modify(Y, Result). > What's wrong??? > 2/ I want to find the maximum of an integer list : > I try this one : > maxList([X], X). > maxList([X|List], Y) :- maxList(List, Z), max(X, Y, Z). > {max is a function wich put in Z the maximum between X and Y}. > In fact, I don't understand why this one work : > maxList([X], X). > maxList([X, Y|List], Result) :- maxList([Y|List], Maxi), max(X, Maxi, > Result). > I hope you can help me! (mainly on the 1/ ) > Thanks. > -- > Vincent
|
Sun, 23 May 2004 00:24:47 GMT |
|
 |
Mike Ringros #5 / 6
|
 2 simple programs
Your maxlist function looked correct; however, I didn't look at too closely. Here's one a wrote for a project a while back and it works. max(X,Y,X) :- X >= Y, !. max(_,Y,Y). maxlist([H],H). maxlist([H1|T],M) :- maxlist(T,R),max(H1,R,M).
Quote: > Hi everybody, > I'm new user of prolog and I meet problems with doing 2 things : > 1/ I want to modify all the occurences of the 'a' character by 'b' in a > list. > I do this but it returns "no" : > modify([], []). > modify([a|List], [b|Result]) :- modify(X, Result). > modify([Z|Y], [Z|Result]) :- modify(Y, Result). > What's wrong??? > 2/ I want to find the maximum of an integer list : > I try this one : > maxList([X], X). > maxList([X|List], Y) :- maxList(List, Z), max(X, Y, Z). > {max is a function wich put in Z the maximum between X and Y}. > In fact, I don't understand why this one work : > maxList([X], X). > maxList([X, Y|List], Result) :- maxList([Y|List], Maxi), max(X, Maxi, > Result). > I hope you can help me! (mainly on the 1/ ) > Thanks. > -- > Vincent
|
Sun, 23 May 2004 11:50:05 GMT |
|
 |
Graham Thwaite #6 / 6
|
 2 simple programs
----------
Quote: >Your maxlist function looked correct; however, I didn't look at too closely. >Here's one a wrote for a project a while back and it works. >max(X,Y,X) :- X >= Y, !. >max(_,Y,Y).
The problem with this definition is that it isn't 'steadfast'. For instance the query ?- max(10,0,0) will succeed because the match with the head of the first clause fails and so it never reaches the '>=' comparison. Consequently the query backtracks to the second clause and (erroneously) succeeds. The correct approach is to place output unifications after the cut e.g. max(X,Y,Z) :- X >= Y, !, Z=X. max(_,Y,Y). O'Keefe addresses this in chapter 3 of his book "The Craft of Prolog". HTH Graham Thwaites
|
Mon, 24 May 2004 23:35:02 GMT |
|
|
|