开发者

scheme - making a circle in eager method

In the next code, we build a circle in Scheme:

 (define make-circle (lambda (x-center y-center radius)
(cons 'circle 
(lambda (m)
        (cond ((eq? m 'x) x-center)
                  ((eq? m 'y) y-center)
        (else radiu开发者_如何转开发s))))))

What the meaning of the variable m? meaning - from where we get the m, and what is mean, for example, the cond: "((eq? m 'x) x-center)".


m most probably means 'a message'. It can be used to fetch corresponding fields of circle data structure, e.g.

(define my-circle (make-circle 1 2 3))

; cdr is here because circle is a cons of 'circle and lambda, 
; better abstract it out in real code
((cdr my-circle) 'y) 

will result in 2. Anonymous function (lambda) tests m to figure out what field you want to fetch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜