From: Ramakrishnan Muthukrishnan Date: Fri, 9 Dec 2011 21:20:51 +0000 (+0530) Subject: solution to 4.14 X-Git-Url: https://git.rkrishnan.org/listings/vdrive/%3C?a=commitdiff_plain;h=97b06c1122821e2453ede0c458b818438cb67ff2;p=sicp.git solution to 4.14 --- diff --git a/src/sicp/ex4_14.rkt b/src/sicp/ex4_14.rkt new file mode 100644 index 0000000..01cf157 --- /dev/null +++ b/src/sicp/ex4_14.rkt @@ -0,0 +1,47 @@ +#lang racket + +#| + +promitive procedures are applied as follows: + +(define (apply-primitive-procedure proc args) + (apply-in-underlying-scheme + (primitive-implementation proc) args)) + +Let us say that we have added `map' as a primitive procedure. It is represented as +('primitive map) + +(primitive-implementation) + +> (define primitive-procedures + (list (list 'car car) + (list 'cdr cdr) + (list 'cons cons) + (list 'null? null?) + (list 'map map) + )) +> (define (primitive-procedure-names) + (map car + primitive-procedures)) +> (primitive-procedure-names) +'(car cdr cons null? map) +> (define (primitive-procedure-objects) + (map (lambda (proc) (list 'primitive (cadr proc))) + primitive-procedures)) +> (primitive-procedure-objects) +'((primitive #) (primitive #) (primitive #) (primitive #) (primitive #)) + +> (apply (primitive-implementation (list 'map map)) (primitive-implementation (list 'car car)) '(((1 2 3) (3 4 5)))) +'(1 3) +> (apply (primitive-implementation (list 'primitive map)) (primitive-implementation (list 'primitive car)) '(((1 2 3) (3 4 5)))) +'(1 3) +> (apply (primitive-implementation (list 'primitive map)) (list 'primitive car) '(((1 2 3) (3 4 5)))) +map: expects type as 1st argument, given: '(primitive #); other arguments were: '((1 2 3) (3 4 5)) +> + +If we apply primitive procedures to our primitive map, it fails because it cannot understand our primitive representation. +In general, all primitive versions of higher order functions fail when passes a primitive procedure. + +If we are implementing Scheme using a Lisp other than Scheme itself (say, Clojure) then we will be passing Scheme functions +to the Clojure's map, which is not guaranteed to work because of the different syntax. +|# \ No newline at end of file