Path: 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 > 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)