2

I need the feature arguments.callee of JavaScript in Racket (Scheme). Do you know how?

Here, an example in JavaScript

function makeFactorialFunc() {
 return function(x) {
   if (x <= 1)
     return 1;
   return x * arguments.callee(x - 1);
 };
}
Alexis King
  • 43,109
  • 15
  • 131
  • 205
Naive Developer
  • 700
  • 1
  • 9
  • 17
  • 2
    Note that `arguments.callee` is not available in JavaScript strict mode. For the same reason that feature is not available in Scheme/Racket. (It makes efficient compilation difficult) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee – soegaard Jul 31 '18 at 11:40
  • Thanks for the comment. I think `arguments.callee` is useful when you need to dynamically wrap a script with functions and call this new function in a recursive way. – Naive Developer Jul 31 '18 at 15:08

1 Answers1

4

You cannot get the currently executing function in a dynamic way in Racket, but you can certainly still implement the function in your question in Racket, just by giving the function a name:

(define (make-factorial-func)
  (define (func x)
    (if (<= x 1)
        1
        (* x (func (- x 1)))))
  func)

It’s possible that you feel like you need the dynamic-ness of arguments.callee for some reason, and it might be possible to achieve that goal through some other mechanism, but seeing as you don’t provide any context for why you think it’s necessary in your question, I can’t guess as to what that other mechanism might be.

Alexis King
  • 43,109
  • 15
  • 131
  • 205