How to obtain my function definition in MIT Scheme

2019-06-22 16:35发布

问题:

In JavaScript, I can retrieve the "source code" definition of a function, for example:

​function alert_Hi() {
    alert("Hi");
}

alert(alert_Hi);

will return exactly what I typed. http://jsfiddle.net/DuCqJ/

How can I do this in MIT Scheme?

I remember seeing something that returns #compound-procedure or something, but what I really want is the "source code".

回答1:

You might try pp

(define (display-hi) (display "Hi"))
(pp display-hi) =>
(named-lambda (display-hi)
  (display "Hi"))

MIT-Scheme debugging aids



回答2:

JavaScript is fully interpreted, so it has full function definitions lying around even after you've defined them. Scheme is not actually fully interpreted; it compiles functions (and a few other constructs, I think) down to a non-readable representation and throws away the initial code.

You could probably get it to store the initial textual representation of a function at runtime using some macro tricks, but I'm inclined to believe that this would be more trouble than it's worth.

If you don't mind me asking, why do you need the textual representation of a defined function at runtime?