{"action":"created","issue":{"url":"https://api.github.com/repos/ruandao/blog/issues/36","labels_url":"https://api.github.com/repos/ruandao/blog/issues/36/labels{/name}","comments_url":"https://api.github.com/repos/ruandao/blog/issues/36/comments","events_url":"https://api.github.com/repos/ruandao/blog/issues/36/events","html_url":"https://github.com/ruandao/blog/issues/36","id":120516408,"number":36,"title":"[12.05] [sicp 2.81]","user":{"login":"ruandao","id":1321779,"avatar_url":"https://avatars.githubusercontent.com/u/1321779?v=3","gravatar_id":"","url":"https://api.github.com/users/ruandao","html_url":"https://github.com/ruandao","followers_url":"https://api.github.com/users/ruandao/followers","following_url":"https://api.github.com/users/ruandao/following{/other_user}","gists_url":"https://api.github.com/users/ruandao/gists{/gist_id}","starred_url":"https://api.github.com/users/ruandao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ruandao/subscriptions","organizations_url":"https://api.github.com/users/ruandao/orgs","repos_url":"https://api.github.com/users/ruandao/repos","events_url":"https://api.github.com/users/ruandao/events{/privacy}","received_events_url":"https://api.github.com/users/ruandao/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-05T00:58:21Z","updated_at":"2015-12-05T03:02:34Z","closed_at":null,"body":"a. With Louis's coercion procedures installed, what happens if `apply-generic` is called with two arguments of type scheme-number or two arguments of type complex for an operation that is not found in the table for those types? For example, assume that we've defined a generic exponentiation operation:\r\n\r\n    (define (exp x y) (apply-generic 'exp x y))\r\nand have put a procedure for exponentiation in the Scheme-number package but not in any other package:\r\n\r\n    ;; following added to Scheme-number package\r\n    (put 'exp '(scheme-number scheme-number)\r\n        (lambda (x y) (tag (expt x y)))) ; using primitive expt\r\n\r\nWhat happens if we call exp with two complex numbers as arguments?\r\n\r\n会陷入递归中...死循环"},"comment":{"url":"https://api.github.com/repos/ruandao/blog/issues/comments/162134644","html_url":"https://github.com/ruandao/blog/issues/36#issuecomment-162134644","issue_url":"https://api.github.com/repos/ruandao/blog/issues/36","id":162134644,"user":{"login":"ruandao","id":1321779,"avatar_url":"https://avatars.githubusercontent.com/u/1321779?v=3","gravatar_id":"","url":"https://api.github.com/users/ruandao","html_url":"https://github.com/ruandao","followers_url":"https://api.github.com/users/ruandao/followers","following_url":"https://api.github.com/users/ruandao/following{/other_user}","gists_url":"https://api.github.com/users/ruandao/gists{/gist_id}","starred_url":"https://api.github.com/users/ruandao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ruandao/subscriptions","organizations_url":"https://api.github.com/users/ruandao/orgs","repos_url":"https://api.github.com/users/ruandao/repos","events_url":"https://api.github.com/users/ruandao/events{/privacy}","received_events_url":"https://api.github.com/users/ruandao/received_events","type":"User","site_admin":false},"created_at":"2015-12-05T03:02:34Z","updated_at":"2015-12-05T03:02:34Z","body":"c. Modify`aply-generic` so that it doesn't try coercion if the two arguments have the same type.\r\n[sicp 2.81](https://gist.github.com/ruandao/af85e246b5f28c829b5d)\r\n\r\n```\r\n(define (apply-generic op . args)\r\n  (let ((type-tags (map type-tag args)))\r\n    (let ((proc (get op type-tags)))\r\n      (if proc\r\n          (apply proc (map contents args))\r\n          (if (= (length type-tags) 2)\r\n              (let ((tag1 (car type-tags))\r\n                    (tag2 (cdr type-tags)))\r\n                (let ((type1->type2 (get-coercion tag1 tag2))\r\n                      (type2->type1 (get-coercion tag2 tag1)))\r\n                  (cond ((same-type? tag1 tag2)\r\n                         (error \"No method for these types -- APPLY-GENERIC\"\r\n                                (list op type-tags)))\r\n                        (type1->type2\r\n                         (apply-generic op (type1->type2 (car args)) (cdr args)))\r\n                        (type2->type1\r\n                         (apply-generic op (car args) (type2->type1 (cdr args))))\r\n                        (else (error \"No method for these types -- APPLY-GENERIC\"\r\n                                     (list op type-tags))))))\r\n              (error \"No method for these types -- APPLY-GENERIC\"\r\n                     (list op type-tags)))))))\r\n```\r\n"}}