Scheme - 使用 `this` 作为 lambda 函数的参数?
Scheme - using `this` as argument of a lambda function?
有人可以澄清一下使用 "this" 关键字背后的概念吗?
(define call
(lambda (obj method-name . args)
(apply (obj method-name) obj args)))
(define -cuboid-
(lambda (w l h)
(define volume
(lambda (this)
(* h (call this 'area))))
(define area
(lambda (this)
(* w l)))
(lambda (method-name)
(cond
((eq? 'volume method-name) volume)
((eq? 'area method-name) area)
(else (error "method not found: ~s" method-name))))
(define r1 (-cuboid- 2 3 4))
(call r1 'area) ;=> 6
(call r1 'volume) ;=> 24
我知道 this
是一个关键字,用来指代正在使用的对象。我发现单独的 this
在这个程序中没有任何特殊意义(它需要引用 lambda 函数的参数)。
调用是((-cuboid- 2 3 4) 'volume)
,它带来(* h (call this 'area))
,this
在哪里定义的?
this
只是lambda
的参数,可以是任何东西;尝试将其更改为,例如,第一个 lambda 中的 myself
和第二个 lambda 中的 me
(顺便说一句,它没有被使用,但需要在那里才能使 call
工作).
调用 ((-cuboid- 2 3 4) 'volume)
returns 该过程,名称根据下面的草图绑定:
在call
中,r1 'volume
调用-cuboid-
的"lookup method",returns调用volume
过程,然后用obj
参数,将其绑定到名称 this
因此,this
绑定到 call
的 r1
参数
有人可以澄清一下使用 "this" 关键字背后的概念吗?
(define call
(lambda (obj method-name . args)
(apply (obj method-name) obj args)))
(define -cuboid-
(lambda (w l h)
(define volume
(lambda (this)
(* h (call this 'area))))
(define area
(lambda (this)
(* w l)))
(lambda (method-name)
(cond
((eq? 'volume method-name) volume)
((eq? 'area method-name) area)
(else (error "method not found: ~s" method-name))))
(define r1 (-cuboid- 2 3 4))
(call r1 'area) ;=> 6
(call r1 'volume) ;=> 24
我知道 this
是一个关键字,用来指代正在使用的对象。我发现单独的 this
在这个程序中没有任何特殊意义(它需要引用 lambda 函数的参数)。
调用是((-cuboid- 2 3 4) 'volume)
,它带来(* h (call this 'area))
,this
在哪里定义的?
this
只是lambda
的参数,可以是任何东西;尝试将其更改为,例如,第一个 lambda 中的 myself
和第二个 lambda 中的 me
(顺便说一句,它没有被使用,但需要在那里才能使 call
工作).
调用 ((-cuboid- 2 3 4) 'volume)
returns 该过程,名称根据下面的草图绑定:
在call
中,r1 'volume
调用-cuboid-
的"lookup method",returns调用volume
过程,然后用obj
参数,将其绑定到名称 this
因此,this
绑定到 call
r1
参数