I am attempting to evaluate a literal as an expression in Scheme (using Guile currently). Example:
(define x '(+ 6 6))
(define y (evaluate-literal x)) ; Expected result: y = 12
(Here, evaluate-literal is a placeholder for what I’m looking for.) Is there a lisp function/idiom that allows this to be done? The reason why I need to do this is because the expression may be invalid at the time of definition, but would be a valid expression later when it is evaluated.
Currently my workaround solution is to use delay and force but it’s not very elegant:
(define x (delay (+ 6 6)))
(define y (force x))
The simplest way would be to use
eval, although difficult to use safely. See this post to see the reasons why.Using the built-in
delay/forceprocedures is a fine solution:Or, as has been suggested in the comments, you could use a
lambdafor implementing your owndelay/forcesyntax:The above is a toy implementation, a real-world implementation would memoize the result for avoiding the need to call the
lambdaeach time, but you get the idea.