# [racket] Is eval very slow?

 From: John Clements (clements at brinckerhoff.org) Date: Thu Jul 19 01:42:03 EDT 2012 Previous message: [racket] Is eval very slow? Next message: [racket] Is eval very slow? Messages sorted by: [date] [thread] [subject] [author]

```On Jul 18, 2012, at 10:15 PM, Joe Gilray wrote:

> Hi,
>
> I've written some code that uses eval:
>
>   (define ops (list * / + -))
>   (define digits '(1 2 3 4 5))
>   (for ([dl (combinations 4 digits)])
>     (define lst '())
>     (for ([d (permute-all dl)])
>       (for* ([o1 ops] [o2 ops] [o3 ops])
>         (unless (and (eq? o1 /) (zero? (eval (list o2 (second d) (list o3 (third d) (fourth d))))))  ; avoid divide by zero
>           (define val (eval (list o1 (first d) (list o2 (second d) (list o3 (third d) (fourth d))))))
>           (when (>= val 1) (set! lst (cons val lst)))) ...
>
> It runs very, very slowly.  Am I doing something wrong?  Is there a way to speed up veal?

You haven't included all of your code, which makes it a bit hard to analyze.

However, based on what you've written here, there seems absolutely no reason to use eval here. Specifically, it looks like

(eval (list o2 (second d) (list o3 (third d) (fourth d))))

… could simply be replaced by

(o2 (second d) (o3 (third d) (fourth d)))

and

(eval (list o1 (first d) (list o2 (second d) (list o3 (third d) (fourth d)))))

… could simply be replaced with

(o1 (first d) (o2 (second d) (o3 (third d) (fourth d))))

Each of these is substantially shorter, and doesn't require eval.

More generally, it looks to me like you're using 'eval' when all you need are higher-order functions. If the variable o1 is bound to *, then I can certainly use (o1 3 4) to compute 12.