Detecting the caller of a function in Scheme or Ra

2019-07-11 21:27发布

问题:

In Scheme or Racket is it possible to detect the caller of a function?

For example, I can write a function to test if a list is a list of atoms as follows:

(define atom? (lambda (x) (and (not (pair? x)) (not (empty? x)))))

(define lat? (lambda (l)
               (define latt?
                 (lambda (l)
                   (cond
                     ((null? l) #t)
                     ((atom? (car l)) (latt? (cdr l)))
                     (else #f))))
               (if (null? l) #f (latt? l))))

but instead of the above, is there a function like "called-by" to do something like this:

(define lat?
  (lambda (l)
    (cond
      ((and (null? l) (called-by "lat?")) #t)
      ((atom? (car l)) (lat? (cdr l)))
      (else #f))))

回答1:

The usual way to do this is to add some argument to the function, or make a loop via an internal definition as you did. Other than that, there is no reliable way to find out the caller of a function.

But in your case, it seems like a good lack of feature -- using it for the above problem is pretty bad. There's nothing wrong with the internal helper version. (It's also quite similar to any other language.)

Finally, I'd expect (lat? null) to return #t since it is a list that has only atoms as elements.



标签: scheme racket