【问题标题】:Structure And Interpretation of Computer Programs Ex2_74计算机程序的结构和解释 Ex2_74
【发布时间】:2021-07-19 23:00:21
【问题描述】:

我正在尝试标记一个列表,以便 file1 变量在 Racket 中如下所示:

(Div1 ((Sam Parnell 100) (Tom Edward 1000) (Rob Hanbury 500) (Andy Springwood 500)))

但是我得到了:

((Sam Parnell 100) (Tom Edward 1000) (Rob Hanbury 500) (Andy Springwood 500))

我在我的包内部使用“标记”来使用 make-file 过程标记一系列记录。我认为它应该遵循第 1.1.3 节中列出的评估模型,但感觉不是这样,这可能与我已经定义 make-file 以采用可能影响的任意数量的参数这一事实有关以我还没有理解的方式评估模型。我还尝试在 Racket 中对此进行调试,但无济于事,因为它在我逐步完成时跳过了它的评估方式,所以我被卡住了。如果有人可以提供帮助,将不胜感激。

#lang sicp

(#%require (only racket/base error))
(#%require (only racket/base make-hash))
(#%require (only racket/base hash-set!))
(#%require (only racket/base hash-ref))

; table set up

(define *op-table* (make-hash))

(define (put op type proc)
  (hash-set! *op-table* (list op type) proc))

(define (get op type)
  (hash-ref *op-table* (list op type) '()))

; data tagging set up

(define (attach-tag type-tag contents) 
  (cons type-tag contents))

(define (type-tag datum) 
  (if (pair? datum) 
      (car datum) 
      (error "Bad tagged datum -- TYPE-TAG" datum)))

(define (contents datum) 
  (if (pair? datum) 
    (cdr datum) 
    (error "Bad tagged datum -- CONTENTS" datum)))

(define (apply-generic op . args) 
  (let ((type-tags (map type-tag args))) 
    (let ((proc (get op type-tags))) 
      (if proc 
          (apply proc (map contents args)) 
          (error 
            "No method for these types -- APPLY-GENERERIC" 
            (list op type-tags))))))

(define (install-Div1-Package) 
  (define (get-name record) 
  (car record))
  (define (get-address record) 
    (cadr record))
  (define (get-salary record) 
    (caddr record))
  (define (make-record name address salary) 
    (list name address salary))
  (define (get-record key file) 
    (cond ((null? file) (error "Employee not in file")) 
          ((eq? key (get-name (car file))) 
           (car file)) 
          (else (get-record key (cdr file)))))
  (define (make-file . records) 
    records)
  ;interface to the rest of the system
 (define (tag x) (attach-tag 'Div1 x)) 
 (put 'get-name '(Div1) get-name)
 (put 'get-address '(Div1) get-address)
 (put 'get-salary '(Div1) get-salary)
 (put 'make-record 'Div1 
      (lambda (name address salary) 
        (make-record name address salary)))
 (put 'get-record '(Div1) get-record)
 (put 'make-file 'Div1 
      (lambda args 
        (tag (make-file args)))))

(install-Div1-Package)

(define (make-record name address salary) 
  ((get 'make-record 'Div1) name address salary))

(define record1 (make-record 'Sam 'Parnell 100))
(define record2 (make-record 'Tom 'Edward 1000))
(define record3 (make-record 'Rob 'Hanbury 500))
(define record4 (make-record 'Andy 'Springwood 500))

record1

(define (make-file . records) 
  (get 'make-file 'Div1) records)

(define file1 (make-file record1 record2 record3 record4))

file1

【问题讨论】:

    标签: scheme evaluation sicp


    【解决方案1】:

    您实际上忘记了调用make-file 过程:

    (define (make-file . records) 
      (get 'make-file 'Div1) ; retrieves the procedure, does nothing with it
      records)               ; return the same input list
    

    另外,因为你想采用任意数量的参数,你需要应用它;这应该工作:

    (define (make-file . records) 
      (apply (get 'make-file 'Div1) records))
    

    【讨论】:

    • 谢谢你的解释,成功了(y)
    猜你喜欢
    • 2016-04-01
    • 2020-07-28
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多