Runtime evaluation/In an environment: Difference between revisions

From Rosetta Code
Content added Content deleted
(Forth EVALUATE)
Line 32: Line 32:
EVALUATE invokes the Forth interpreter on the given string.
EVALUATE invokes the Forth interpreter on the given string.
<lang forth>
<lang forth>
: f s" dup *" evaluate ;
: f-" ( a b snippet" -- )
[char] " parse ( code len )
2dup 2>r evaluate
swap 2r> evaluate
- . ;


3 f . \ 9
2 3 f-" dup *" \ 5 (3*3 - 2*2)
2 f . \ 4
</lang>
</lang>
This can be used to treat a data stream as code, or to provide a lightweight macro facility when used in an IMMEDIATE word.
This can be used to treat a data stream as code, or to provide a lightweight macro facility when used in an IMMEDIATE word.

Revision as of 21:59, 16 April 2009

Task
Runtime evaluation/In an environment
You are encouraged to solve this task according to the task description, using any language you may know.

Given a program in the language representing a function, evaluate it with the variable x (or another name if that is not valid) bound to a provided value, then evaluate it again with x bound to another provided value, then subtract the result of the first from the second and return or print it.

Preferably, do so in a way which does not involve string manipulation of source code, and is plausibly extensible to a runtime-chosen set of bindings.

For more general examples and language-specific details, see Eval.

Common Lisp

<lang lisp> (defun eval-with-x (program a b)

 (let ((at-a (eval `(let ((x ',a)) ,program)))
       (at-b (eval `(let ((x ',b)) ,program))))
   (- at-b at-a)))

</lang>

<lang lisp> (eval-with-x '(exp x) 0 1) => 1.7182817 </lang>

This version ensures that the program is compiled, once, for more efficient execution:

<lang lisp> (defun eval-with-x (program a b)

 (let* ((f (compile nil `(lambda (x) ,program)))
        (at-a (funcall f a))
        (at-b (funcall f b)))
   (- at-b at-a)))

</lang>

Forth

EVALUATE invokes the Forth interpreter on the given string. <lang forth>

f-" ( a b snippet" -- )
 [char] " parse   ( code len )
 2dup 2>r evaluate
 swap 2r> evaluate
 - . ;

2 3 f-" dup *" \ 5 (3*3 - 2*2) </lang> This can be used to treat a data stream as code, or to provide a lightweight macro facility when used in an IMMEDIATE word. <lang forth>

:macro ( "name <char> ccc<char>" -- )
 : [CHAR] ; PARSE  POSTPONE SLITERAL  POSTPONE EVALUATE
 POSTPONE ; IMMEDIATE
macro times 0 do ;
test 8 times ." spam " loop ;

see test

test
 8 0 
 DO     .\" spam " 
 LOOP
 ; ok

</lang>

Octave

<lang octave>p = "x .* 2"; x = [1:3]; a = eval(p); x = [4:6]; b = eval(p); disp(b-a);</lang>

Output:

6   6   6

Perl

<lang perl>sub eval_with_x

  {my $code = shift;
   my $x = shift;
   my $first = eval $code;
   $x = shift;
   return eval($code) - $first;}

print eval_with_x('3 * $x', 5, 10), "\n"; # Prints "15".</lang>

PHP

<lang php><?php function eval_with_x($code, $a, $b) {

   $x = $a;
   $first = eval($code);
   $x = $b;
   $second = eval($code);
   return $second - $first;

}

echo eval_with_x('return 3 * $x;', 5, 10), "\n"; # Prints "15". ?></lang>

Python

<lang python>>>> def eval_with_x(code, a, b): return eval(code, {'x':b}) - eval(code, {'x':a})

>>> eval_with_x('2 ** x', 3, 5) 24</lang>

Python: for multiple names

A slight change allows the evaluation to take multiple names: <lang python>>>> def eval_with_args(code, **kwordargs): return eval(code, kwordargs)

>>> code = '2 ** x' >>> eval_with_args(code, x=5) - eval_with_args(code, x=3) 24 >>> code = '3 * x + y' >>> eval_with_args(code, x=5, y=2) - eval_with_args(code, x=3, y=1) 7 >>> </lang>

Ruby

<lang ruby>def getBinding(x)

 binding

end

def eval_with_x(code, a, b)

 eval(code, getBinding(b)) - eval(code, getBinding(a))

end

puts eval_with_x('2 ** x', 3, 5) # Prints "24"</lang>

Scheme

Almost identical to the Common Lisp version above. <lang scheme>(define (eval-with-x prog a b)

 (let ((at-a (eval `(let ((x ',a)) ,prog)))
       (at-b (eval `(let ((x ',b)) ,prog))))
   (- at-b at-a)))</lang>

Tcl

<lang tcl>package require Tcl 8.5

proc eval_twice {func a b} {

   set 1st [apply $func $a]
   set 2nd [apply $func $b]
   expr {$2nd - $1st}

}

eval_twice {x {expr {2 ** $x}}} 3 5 ;# ==> 24</lang>