Subject: Re: [SWIPL] About Applying a predicate to all the elements of a list

From: "Richard A. O'Keefe" <[email-masked]>
Date: Fri, 15 Dec 2000 14:05:32 +1300 (NZDT)

Rastei Amelia Viorela <[email-masked]> wrote:
> Sorry, previous question was not clear at all!
> I reformulate:
> How does maplist actually work? 
	
You don't need to know that.
What you need to know is what it *means*.

The maplist/N family of predicates, designed by Lawrence Byrd and me,
can be explained as

    maplist(P, [X11,...,X1n], ..., [Xm1,...,Xmn]) :-
	P(X11, ..., Xm1),
	...
	P(Xm1, ..., Xmn).

For the sake of argument, let's invent an :- index declaration, which
says to build indices on each of the specified arguments independently,
and to use the first one in numerical order for which the actual argument
is non-variable.  Then we'd have

    :- index maplist(_,1).

    maplist(P, []).
    maplist(P, [X1|X1s]) :-
        call(P, X1),
        maplist(P, X1s).

    :- index maplist(_,1,2).

    maplist(P, [], []).
    maplist(P, [X1|X1s], [X2|X2s]) :-
        call(P, X1, X2),
        maplist(P, X1s, X2s).

    :- index maplist(_,1,2,3).

    maplist(P, [], [], []).
    maplist(P, [X1|X1s], [X2|X2s], [X3|X3s]) :-
        call(P, X1, X2, X3),
        maplist(P, X1s, X2s, X3s).

As for the call/N family (invented by me and Alan Mycroft),
it's defined *as if*

    call(P(X1,...,Xm), Y1, ..., Yn) :- P(X1, ..., Xm, Y1, ..., Yn).

There are many ways to implement that, some faster than others.

	    I am trying to implement MINIMAX algotithm. For Tic tac Toe.

Is that the same as noughts and crosses?

The important thing is that the first argument of call/(N+1) or
maplist/(N+1) is a callable term representing a call to a predicate
that is missing the *last* N arguments.  (Why the last?  Because of
the general inputs-before-outputs style rule in Prolog.)