読者です 読者をやめる 読者になる 読者になる

(wat-aro)

無職から有職者にランクアップしました

SICP 問題 4.14

scheme SICP
;; M-EVAL中の手続きは先頭に'procedureがついたリスト.
;; 基層のLispのmapを使うとただのリストとして受け取ってしまうためにうまくいかない.


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

(define (primitive-procedure-objects)
  (map (lambda (proc) (list 'primitive (cadr proc)))
       primitive-procedures))

(define (setup-environment)
  (let ((initial-env
         (extend-environment (primitive-procedure-names)
                             (primitive-procedure-objects)
                             the-empty-environment)))
    (define-variable! 'true true initial-env)
    (define-variable! 'false false initial-env)
    initial-env))

(define the-global-environment (setup-environment))
gosh> (driver-loop)


;;; M-EVAL input:
(map car '((a 1) (b 2) (c 3) (d 4)))
*** ERROR: invalid application: ((primitive #<subr car>) (a 1))
Stack Trace:
_______________________________________
  0  (eval input the-global-environment)
        At line 961 of "/Users/home/work/scheme/SICP/4.1.scm"
  1  (eval expr env)
        At line 179 of "/usr/local/Cellar/gauche/0.9.4/share/gauche-0.9/0.9.4/lib/gauche/interactive.scm"
(define primitive-procedures
  (list (list 'car car)
        (list 'cdr cdr)
        (list 'cons cons)
        (list 'null? null?)))
gosh> (driver-loop)


;;; M-EVAL input:
(define (map proc lst)
  (if (null? lst)
      '()
      (cons (proc (car lst))
            (map proc (cdr lst)))))

;;; M-EVAL value
ok

;;; M-EVAL input:
(map car '((a 1) (b 2) (c 3) (d 4)))

;;; M-EVAL value
(a b c d)