以下是SICP的练习.我无法自己解决这个问题.可以帮助我理解一些吗?
在interpreator中输入以下代码:
(car ''abracadabra)
Run Code Online (Sandbox Code Playgroud)
它打印出'引用'.为什么?
在下面的代码中,我试图了解变量如何whatami获得其值.在遵循逻辑的过程中,我看到过程(lambda (y) (/ x y))是我传递给方法的参数average-damp,并在该方法中表示为变量f.似乎(/ x y)并且(average (f whatami) whatami)需要执行,但我无法弄清楚执行的顺序.任何帮助表示赞赏.
(define (average x y)
(/ (+ x y) 2))
(define (fixed-point f start)
(define tolerance 0.00001)
(define (close-enuf? u v)
(< (abs (- u v)) tolerance))
(define (iter old new)
(if (close-enuf? old new)
new
(iter new (f new))))
(iter start (f start)))
(define average-damp
(lambda (f)
(lambda (whatami) (average (f whatami) whatami))))
; square root with average …Run Code Online (Sandbox Code Playgroud) 这是SICP中的一个示例,我输入它但是有错误.
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
Run Code Online (Sandbox Code Playgroud)
这是错误:
函数调用:期望一个函数在打开括号后,但找到一个变量
我目前正在研究SICP的练习1.29,我的程序一直给我以下错误:
+: expects type <number> as 2nd argument, given: #<void>; other arguments were: 970299/500000
这是我正在使用的代码racket:
(define (cube x)
(* x x x))
(define (integral2 f a b n)
(define (get-mult k)
(cond ((= k 0) 1)
((even? k) 4)
(else 2)))
(define (h b a n)
(/ (- b a) n))
(define (y f a b h k)
(f (+ a (* k (h b a n)))))
(define (iter f a b n k)
(cond ((> n k) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Racket中实现一些SICP图形程序,但有两个问题:
当我需要使用'let'时,我不能使用初学者语言.当我尝试更改语言,或使用"高级"语言打开新文件时,我收到此错误:
module: identifier already imported from a different source
Run Code Online (Sandbox Code Playgroud)
我尝试加载图像模块时出错(需要2htdp/image).
这是怎么回事?另外,有没有更好的方法来训练图像中的图像?
Monad当我搜索SICP第二版书时,我找不到" " 这个词.SICP的哪些概念(或章节)与Monad有关?
这是来自SICP的生成递归过程的因子过程.
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
Run Code Online (Sandbox Code Playgroud)
现在这是相同的过程,但会生成一个迭代过程.计数器增加到n,并且产品在每个过程调用中通过计数器自行增加.当不在块结构中时,fact-iter具有变量max-count,实际上是n.
(define (factorial n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1))
Run Code Online (Sandbox Code Playgroud)
我有点好奇为什么我们需要反击,它实际上并没有做任何事情,只是增加自己并使用它的值来测试基本情况.和递归过程一样,我们不能只是添加一个累加器来迭代进行同样的过程吗?例如:
(define (factorial n)
(define (fact-iter product n)
(if (= n 1)
product
(fact-iter (* n product)
(- n 1))))
(fact-iter 1 n))
Run Code Online (Sandbox Code Playgroud)
所以,这仍然是一个迭代过程,我认为比第一个例子更明显的过程.
但是,这本书首先应该是第一个例子.那么第一个迭代示例相对于第二个过程的优势是什么?
我正在尝试将SICP的元循环评估程序转换为Clojure.在setup-environment调用extend-environment不编译因为我收到错误"尝试调用未绑定的fn".这是代码的一部分:
(... loads of methods for creating and managing environment list)
(def primitive-procedures
(list (list 'car first)
(list 'cdr rest)
(list 'cons conj) ;; TODO: reverse
(list 'null? nil?)
(list 'list list)
(list '+ +)
(list '- -)
(list '* *)
(list '/ /)
;; more primitives
))
(def primitive-procedure-names
#(map [first
primitive-procedures]))
(def primitive-procedure-objects
(fn [] (map (fn [p] (list 'primitive (second p)))
primitive-procedures)))
(def the-empty-environment '())
(defn extend-environment [vars vals base-env]
(if …Run Code Online (Sandbox Code Playgroud) 我正在使用Racket和Dr. Racket进行教育.
在变量"x"和"y"的以下定义之后:
(define x (list 1 2 3))
(define y (list 4 5 6))
Run Code Online (Sandbox Code Playgroud)
我决定使用这些变量创建3个不同的列表.
第一:
(append x y)
>> '(1 2 3 4 5 6)
Run Code Online (Sandbox Code Playgroud)
第二:
(cons x y)
>> '((1 2 3) 4 5 6)
Run Code Online (Sandbox Code Playgroud)
第三:
(list x y)
>> ((1 2 3) (4 5 6))
Run Code Online (Sandbox Code Playgroud)
之后,我决定在三个列表中使用布尔运算符"and"和"or".令我感到惊讶的是输出.为什么会这样?为什么"或"和"和"选择其中一个列表?这个决定背后的比例是多少?
(and (append x y) (cons x y) (list x y))
>> '((1 2 3) (4 5 6))
(or (append x y) (cons x y) (list x y))
>> '(1 2 …Run Code Online (Sandbox Code Playgroud) 我正在研究SICP,编写了两个过程来计算1 / n ^ 2的总和,第一个过程生成一个递归过程,第二个过程生成一个迭代过程:
(define (sum-rec a b)
(if (> a b)
0
(exact->inexact (+ (/ 1 (* a a)) (sum-rec (1+ a) b)))))
(define (sum-it a b)
(define (sum_iter a tot)
(if (> a b)
tot
(sum_iter (1+ a) (+ (/ 1 (* a a)) tot))))
(exact->inexact (sum_iter a 0)))
Run Code Online (Sandbox Code Playgroud)
我测试了两个过程在使用较小的值调用时给出的结果完全相同b,并且结果接近$ pi ^ 2/6 $ b,并且随着预期的增大而变大。
但是令人惊讶的是,呼叫(sum-rec 1 250000)几乎是瞬时的,而呼叫却(sum-it 1 250000)要花很长时间。
有什么解释吗?