Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!europa.eng.gtefsd.com!news.mathworks.com!solaris.cc.vt.edu!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!lee
From: lee@munta.cs.mu.OZ.AU (Lee Naish)
Subject: Re: Prolog standardization -- What now?
Message-ID: <9430812.9943@mulga.cs.mu.OZ.AU>
Sender: news@cs.mu.OZ.AU
Organization: Department of Computer Sci, University of Melbourne
References: <3961lo$kdu@lightning.ditc.npl.co.uk> <9430711.8652@mulga.cs.mu.OZ.AU> <39afgj$n1q@newshost.uni-koblenz.de>
Date: Fri, 4 Nov 1994 01:27:14 GMT
Lines: 62

In article <39afgj$n1q@newshost.uni-koblenz.de> stolzen@informatik.uni-koblenz.de (Frieder Stolzenburg) writes:
>In article <9430711.8652@mulga.cs.mu.OZ.AU>, lee@munta.cs.mu.OZ.AU (Lee Naish)
>writes:
>
>What is the/your definition of map, filter, foldr, call/n?

The NU-Prolog code follows.  call/N is builtin, as it should be in all
Prolog systems.

:- pred map(void(A, B), list(A), list(B)).
:- map(F, As0, As) when As0 or As.      % reversible
map(_F, [], []).
map(F, A0.As0, A.As) :-
        call(F, A0, A),
        map(F, As0, As).

:- pred filter(void(A), list(A), list(A)).
:- filter(_, [], _) when ever.
:- filter(_P, _A0._, _) when ever. % could use A0 and P
filter(_P, [], []).
filter(P, A0.As0, As) :-
        (if call(P, A0) then    % could use -> for portability+speed
                As = A0.As1
        else
                As = As1
        ),
        filter(P, As0, As1).

:- pred foldr(void(A, B, B), B, list(A), B).
:- foldr(_, _, As, _) when As.
foldr(F, B, [], B).
foldr(F, B, A.As,, R) :-
        foldr(F, B, As, R1),
        call(F, A, R1, R).

Examples (plus/3 and append/3 are builtin):

?- map(plus(1), [1,2,3], X).
X = [2, 3, 4]
?- map(plus(1), X, [2,3,4]).
X = [1, 2, 3]
filter(>(3), [3,2,5,1,4], X).
X = [2, 1]
?- foldr(append, [], [[1,2], [], [3]], X).
X = [1, 2, 3]
?- map(plus, [1,2,3], X).
Undefined predicate: plus/2

The last example shows how the definitions above are not as flexible
as what you get in a modern functional language.  In NUE-Prolog
(http://www.cs.mu.oz.au/~lee/papers/eq/index.html) you can do the
following:

?= map(plus(1), [1,2,3]).
[2, 3, 4]
?= map(plus, [1,2,3]).
[plus(1), plus(2), plus(3)]  % list of fns from num to num


	lee


