我接受了以下练习作为家庭作业。我坐了几个小时没有任何成功,所以我别无选择,只能使用你的帮助。
示例:
(define m1 (cons "fixNumber" (lambda () 42)))
(define m3 (cons "add" (lambda (x y) (+ x y))))
(define myobj (create-obj (list m1 m2 m3)))
(myobj "fixNumber" '()) ;; => 42
(myobj "add" '(1 2)) ;; => 3
(myobj "myMethod" '()) ;; => "Error: no such method"
答案 0 :(得分:1)
这应该做:
(define (create-obj mlist)
(lambda (method parms)
(let ((func (assoc method mlist)))
(if func
(apply (cdr func) parms)
"Error: no such method"))))