Results 1 to 3 of 3

Thread: [Prolog] evaluate string to expression

  1. #1
    Join Date
    Apr 2009
    Location
    the Netherlands
    Beans
    150
    Distro
    Ubuntu 10.04 Lucid Lynx

    [Prolog] evaluate string to expression

    Prolog newbie here. I don't know if Prolog's actually popular at any place, but I have a simple question.

    Is it possible in Prolog to have a function evaluatefun like
    Code:
    fun(Cm, Meters, Result) :-
    	Result is Cm * Meters.
    
    evaluatefun( (fun(arg1, arg2, Result)), 50, 10) :- ...
    Where (fun(arg1, arg2, Result) (put as first parameter to evaluatefun) is actually passed as a string/constant - and then fun (the function itself) is evaluated with parameters 50 and 10 as first two parameters.

    So (fun(arg1, arg2, Result) is a string/constant put as first parameter to evaluatefun, but the idea is to evaluate/call/run the passed string/constant ánd parameters, as the related existing function with parameters.

  2. #2
    Join Date
    Nov 2009
    Location
    West Bengal, India
    Beans
    30
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: [Prolog] evaluate string to expression

    call predicate is what you are asking for.
    Code:
    call(fun,10,2,Res).
    will do the job.
    "Simple is better than complex.
    Complex is better than complicated."
    http://www.debsankha.net/

  3. #3
    Join Date
    Apr 2009
    Location
    the Netherlands
    Beans
    150
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Prolog] evaluate string to expression

    Thanks, I have it working now with

    Code:
    costfun(Cm, Meters, Result) :-
    	Result is Cm * Meters.
    
    evaluatefun([Fun,[X, Y, Result]], A, B) :-
    	call(Fun,A,B,Result).
    Code:
    ?- evaluatefun([costfun,[Arg1,Arg2,Result]], 50, 10).
    Result = 500.
    Although, now Fun does have to be passed as [Fun,[X, Y, Result]] instead of (Fun,(X, Y, Result)).

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •