Re: call as a substitute for macros or functional programming?
Carlo Capelli <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CABty9wzgP-V=zNz88X=ku3hq04xbkfQupa0pP3E12Lxcu0nJYQ@mail.gmail.com> |
call/n is ok, but you should make explicit to the compiler what arguments
will be predicates, adding (for instance)
:- meta_predicate silly(+,+,2).
Otherwise, there are other approaches - see
lambda<http://www.swi-prolog.org/pack/list?p=lambda>,
or func <http://www.swi-prolog.org/pack/list?p=func> contributed packages.
I have my own, that it's an enhancement of an older approach, in turn
derived from grips2 (by J.Paine).
I call it lifter, because of the simple transformation it applies (lift up
any placeholder #). Here is a factorial:
fac(N, F) :- N > 1 -> F is fac(# is N-1, #) * N ; F is 1.
Lifter allows naming the lifted variable(s) for reuse. It seems to play
well with lambda
delta_fields(FA, FB, FD) :-
append(maplist(\X^Y^(Y = +X),
ord_subtract(sort(FA, #A), sort(FB, #B), #), #)
, maplist(\X^Y^(Y = -X), ord_subtract(B, A, #), #) ,FD).
test(4) :- delta_fields([1,2,3,4], [1,5,2], X), X = [+ 3, + 4, - 5].
A problem with this kind of code, that relies on goal_expansion, it's the
added difficulty to debug. The compiler cannot recover the source (at
least, not every time), and stepping inside it's a bit of nightmare.
HTH Carlo
2013/7/10 Ross Boylan <[email protected]>
> I have code that I'm using with various small modifications; at the moment
> I do
> this by copying, pasting and editing. In other languages I could avoid
> this
> by passing functions into functions or using a macro/template system.
>
> It looks as if call/n will give me something like functional programming,
> e.g.,
> silly(Src, Dest, Op) :- call(Op, Src, Dest).
>
> ?- silly(path([ostempserver1]), S, content_for).
> gives the same result as
> ?- content_for(path([ostempserver1]), D).
>
> Is this the right approach? Are there others I should consider?
>
> Thanks.
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
-------------- next part --------------
HTML attachment scrubbed and removed