本文介绍了方案 - 使用申请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到了以下练习作为家庭作业.我在上面坐了几个小时都没有成功,所以我别无选择,只能求助于您.
I received the following exercise as homework. I sat on it for hours without any success, so I have no choice but to use your help.
示例:
(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"
推荐答案
应该这样做:
(define (create-obj mlist)
(lambda (method parms)
(let ((func (assoc method mlist)))
(if func
(apply (cdr func) parms)
"Error: no such method"))))
这篇关于方案 - 使用申请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!