Article View: comp.lang.prolog
Article #790Re: taking a predicate name as an argument
From: Peter G Ludemann
Date: Sat, 14 Apr 1990 07:20
Date: Sat, 14 Apr 1990 07:20
35 lines
1679 bytes
1679 bytes
> how do i take a predicate name as an argument and use it as a basis for
> selecting an operation on the other arguments?
> [ e.g.: question('+', X, Y, Answer) :- Answer is X + Y . ]
> i would like question to be general enough to take any operator and perform
> it on X and Y resulting in Answer.
The general solution to this question in IBM-Prolog is
(using its Edinburgh syntax):
question(Pred, X, Y, Answer) :- Answer is Pred(X,Y) .
or even more compactly using its standard syntax:
question(Pred, X, Y, ? Pred(X,Y)) .
However, this solution is not readily available in other
Prologs because:
- Functor names can't be variables. It would seem that this
could be overcome by using =../2:
question(Pred, X, Y, Answer) :-
F =.. [Pred, X, Y],
Answer is F .
- But general terms aren't allowed with "is" (at least, this is
the case with Quintus 2.x; I assume it's true of others); if
you try this, you will get a run-time error for is/2.
So, the answer would seem to be "it can't be done" in many
popular Prologs; you'll just have to list out all the arithmetic
operators yourself (there aren't too many, fortunately).
Peter Ludemann IBM Prolog Development (these are my opinions, not IBM's)
Technical Details
Message-ID:
<28908@cup.portal.com>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!fernwood!portal!cup.portal.com!pglReferences:
<35047@brunix.UUCP> <2624D004.805@orion.oac.uci.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!fernwood!portal!cup.portal.com!pgl From: pgl@cup.portal.com (Peter G Ludemann) Newsgroups: comp.lang.prolog Subject: Re: taking a predicate name as an argument Message-ID: <28908@cup.portal.com> Date: 14 Apr 90 07:20:30 GMT References: <35047@brunix.UUCP> <2624D004.805@orion.oac.uci.edu> Organization: The Portal System (TM) Lines: 35