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.
精彩评论