Ford O.
Ford O.

Reputation: 1498

Scheme : How to quote result of function call?

Is it possible to quote result of function call?

For example

(quoteresult (+ 1 1)) => '2

Upvotes: 0

Views: 518

Answers (1)

Alexis King
Alexis King

Reputation: 43852

At first blush, your question doesn’t really make any sense. “Quoting” is a thing that can be performed on datums in a piece of source code. “Quoting” a runtime value is at best a no-op and at worst nonsensical.

The example in your question illustrates why it doesn’t make any sense. Your so-called quoteresult form would evaluate (+ 1 1) to produce '2, but '2 evaluates to 2, the same thing (+ 1 1) evaluates to. How would the result of quoteresult ever be different from ordinary evaluation?

If, however, you want to actually produce a quote expression to be handed off to some use of dynamic evaluation (with the usual disclaimer that that is probably a bad idea), then you need only generate a list of two elements: the symbol quote and your function’s result. If that’s the case, you can implement quoteresult quite simply:

(define (quoteresult x)
  (list 'quote x))

This is, however, of limited usefulness for most programs.

For more information on what quoting is and how it works, see What is the difference between quote and list?.

Upvotes: 2

Related Questions