Detecting the caller of a function in Scheme or Racket

拈花ヽ惹草 提交于 2019-12-10 22:36:19

问题


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.



来源:https://stackoverflow.com/questions/7961749/detecting-the-caller-of-a-function-in-scheme-or-racket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!