max element of list in prolog

This is a discussion on max element of list in prolog within the General AI Programming forums, part of the Cprogramming.com and AIHorizon.com's Artificial Intelligence Boards category; Maybe I should post at Tech board? I want to find the max element of a list, in Prolog . ...

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681

    max element of list in prolog

    Maybe I should post at Tech board?

    I want to find the max element of a list, in Prolog.

    Here is my code (see comments)
    Code:
    maxList([], M).
    maxList([X|L], M):-
    	M is X,
    		maxList2(L, M).
    maxList2([], M):-writeln(M). /* HERE I HAVE THE CORRECT RESULT! */
    /* But the output is going to remain set at the 1st element of the
     list, no matter what! */
    maxList2([X|L], M):-
    	X > M,
    		M1 is X,
    			maxList2(L, M1).
    maxList2([X|L], M):-
    	X =< M,
    		maxList2(L, M).
    Any ideas ?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    I started from scratch and finally I came up with this code
    Code:
    maxOfTwo(X, Y, X) :-
    	X >= Y.
    maxOfTwo(X, Y, Y) :-
    	X < Y.
    
    maxL([X], X).
    maxL([H | T], X) :-
    	maxL(T, I),
    		maxOfTwo(H, I, X).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    I think your second version is pretty good. It's been a while since I wrote any Prolog though.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    The first was not ok. When backtracking, the result could not be maintained. I finished with the program way before the deadline, so I wrote another predicate (an iterative method - the max is actually the maxOfTwo I have above) for finding max element of a list in prolog. Here it is:
    Code:
    maxlist1([X | List], Max):-
          maxlist2(List, X, Max).
    maxlist2([], Max, Max).
    maxlist2([X | List], SoFarMax, Max):-
          max(X, soFarMax, NewSoFarMax),
                maxlist2(List, NewSoFarMax, Max).
    
    /***** and the max, instead of the maxOfTwo I have above,
    could be written like an if-else statement, with the cut we learned
    at this course ********/
    max(X, Y, X):- X >= Y, !.
    max(X, Y, Y).
    Where did you have to write prolog brewbuck? I mean where prolog was helpful for you?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by std10093 View Post
    Where did you have to write prolog brewbuck? I mean where prolog was helpful for you?
    I used it in a couple of applications as a sort of "reverse parser" to generate all strings produced by a given grammar. It's been a while!
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Pretty interesting...
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2013, 05:11 AM
  2. How to get last element of list?
    By dnguyen1022 in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2009, 11:48 AM
  3. Need help with returning and element of a std::list
    By chadsxe in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2009, 02:35 PM
  4. Remove element from STL list
    By Micko in forum C++ Programming
    Replies: 7
    Last Post: 07-05-2007, 11:07 AM
  5. list first element
    By wyrwidab in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2005, 11:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21