From 97b06c1122821e2453ede0c458b818438cb67ff2 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Sat, 10 Dec 2011 02:50:51 +0530 Subject: [PATCH] solution to 4.14 --- src/sicp/ex4_14.rkt | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/sicp/ex4_14.rkt 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 #<procedure:car>) (primitive #<procedure:cdr>) (primitive #<procedure:cons>) (primitive #<procedure:null?>) (primitive #<procedure:map>)) + +> (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 <procedure> as 1st argument, given: '(primitive #<procedure:car>); 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 -- 2.45.2