Scheme - using apply

℡╲_俬逩灬. 提交于 2019-12-13 05:25:24

问题


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.

Examples:

(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" 

回答1:


This should do:

(define (create-obj mlist)
  (lambda (method parms)
    (let ((func (assoc method mlist)))
      (if func
          (apply (cdr func) parms)
          "Error: no such method"))))


来源:https://stackoverflow.com/questions/27514132/scheme-using-apply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!