如何在 SICP、Scheme、练习 2.78 等中获得 put 和 get 函数 [英] How do I get the functions put and get in SICP, Scheme, Exercise 2.78 and on
问题描述
我正在尝试在 SICP 中做练习 2.78,但是 put 和 get 函数未知.我尝试了多种语言,比如相当大、球拍、r5rs、mit-scheme、mzscheme 等.我什至下载了 SICP 支持(http://www.neilvandyke.org/sicp-plt/),但无济于事.如何让这些功能发挥作用?
I am trying to do exercise 2.78 in SICP, but the functions put and get are unknown. I have tried multiple languages, like pretty big, racket, r5rs, mit-scheme, mzscheme, etc. I even downloaded the SICP support (http://www.neilvandyke.org/sicp-plt/), to no avail. How can I get these functions to work?
推荐答案
是的,我发现 SICP 有时会因为这样的事情而有点烦人.假定存在但实际上不存在的函数使尝试示例变得更加困难.我像这样写了我自己的 (get) 和 (put)(这是在 GNU guile 中):
Yes, I found SICP a little annoying sometimes because of stuff like this. Functions that are assumed to exist but don't actually exist make it harder to try to the examples. I wrote my own (get) and (put) like so (this was in GNU guile):
(define global-array '())
(define (make-entry k v) (list k v))
(define (key entry) (car entry))
(define (value entry) (cadr entry))
(define (put op type item)
(define (put-helper k array)
(cond ((null? array) (list(make-entry k item)))
((equal? (key (car array)) k) array)
(else (cons (car array) (put-helper k (cdr array))))))
(set! global-array (put-helper (list op type) global-array)))
(define (get op type)
(define (get-helper k array)
(cond ((null? array) #f)
((equal? (key (car array)) k) (value (car array)))
(else (get-helper k (cdr array)))))
(get-helper (list op type) global-array))
从本书后面的角度来看,这可能是一个幼稚的实现,但相当简单且运行良好.
Probably a naive implementation from the perspective of later in the book, but fairly simple and worked fine.
这篇关于如何在 SICP、Scheme、练习 2.78 等中获得 put 和 get 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!