Higher order programming: apply/3 implemented in swi
- From: "levilista@xxxxxxxxx" <levilista@xxxxxxxxx>
- Date: Sun, 18 May 2008 00:03:50 -0700 (PDT)
The apply/3 predicate is crucial for higher order programming in
prolog.
Maybe I'm just reinventing the wheel, but I haven't found apply/3 in
the manual, or on other prolog sites. And as I saw on this group's
archives, there were some unresolved thread relating to it.
I wrote it, as it was specified here:
http://www.cs.mu.oz.au/~lee/papers/ho/
I copy my code here:
%apply(f(a,b),[c,d,e],f(a,b,c,d,e))
apply(F,A,FA):-
functor(F,Atom,Argc),
countlist(Argc,Argindexes),reverse(Argindexes,Argindexes1),
findall(X,(member(Y,Argindexes1),arg(Y,F,X)),Y),
append(Y,A,YA),
length(YA,Argc1),
functor(FA,Atom,Argc1),
dressup(Argc1,FA,YA).
dressup(0,_,_):-!.
dressup(Index,Expr,ParamList):-
elementat(Index,ParamList,Param),arg(Index,Expr,Param),
I1 is Index-1,dressup(I1,Expr,ParamList).
% countlist(?C,?L)
%L contains numbers from C to 1
countlist(0,[]):-!.
countlist(C,[C|T]):-
C1 is C-1, countlist(C1,T).
elementat(1,[H|_],H):-!.
elementat(C,[_|T],R):- C1 is C-1, elementat(C1,T,R).
.
- Follow-Ups:
- Re: Higher order programming: apply/3 implemented in swi
- From: Jan Wielemaker
- Re: Higher order programming: apply/3 implemented in swi
- Prev by Date: Re: get unused integer
- Next by Date: Re: Higher order programming: apply/3 implemented in swi
- Previous by thread: get unused integer
- Next by thread: Re: Higher order programming: apply/3 implemented in swi
- Index(es):