sicp每日一题[2.53-2.55]
2.53不用写代码,2.54和2.55属于一道题,所以就放到一起吧
Exercise2.53
What would the interpreter print in response to evaluating each of the following expressions?
(list 'a 'b 'c)
(list (list 'george))
(cdr '((x1 x2) (y1 y2)))
(cadr '((x1 x2) (y1 y2)))
(pair? (car '(a short list)))
(memq 'red '((red shoes) (blue socks)))
(memq 'red '(red shoes blue socks))
结果如下:
'(a b c)
'((george))
'((y1 y2))
'(y1 y2)
#f
#f
'(red shoes blue socks)
Exercise 2.54
Two lists are said to be equal? if they contain equal elements arranged in the same order. For example,
(equal? '(this is a list) '(this is a list))
is true, but
(equal? '(this is a list) '(this (is a) list))
is false. To be more precise, we can define equal? recursively in terms of the basic eq? equality of symbols by saying that a and b are equal? if they are both symbols and
the symbols are eq?, or if they are both lists such that(car a)
is equal? to(car b)
and(cdr a)
is equal? to(cdr b)
. Using this idea, implement equal? as a procedure.
这道题挺简单的,依次比较就行了。
(define (equal? a b)
(cond ((and (null? a) (null? b)) true)
((or (null? a) (null? b)) false)
((eq? (car a) (car b)) (equal? (cdr a) (cdr b)))
(else false)))
(equal? '(this is a list) '(this is a list))
(equal? '(this is a list) '(this (is a) list))
; 执行结果
#t
#f
Exercise 2.55
Eva Lu Ator types to the interpreter the expression
(car ''abracadabra)
To her surprise, the interpreter prints back quote. Explain.
因为
(car ''abracadabra)
被解释器理解为(car (quote (quote abracadabra)))
,第一个 quote 引用了后面的内容(quote abracadabra)
,
这实际上是一个有2个元素的 list,对这个list 调用 car 就取出了第一个元素 quote。也就是第二个 quote 没有被当作函数使用,而是被当作字符串了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?