首页 > 其他分享 >sicp每日一题[2.24-2.27]

sicp每日一题[2.24-2.27]

时间:2024-09-24 10:34:19浏览次数:6  
标签:reverse items list sicp deep cdr car 2.27 2.24

2.24-2.26没什么代码量,所以跟 2.27 一起发吧。

Exercise 2.24

Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in Figure 2.6).


解释器执行结果为:'(1 (2 (3 4)))
另外两种表示方法见下图:


Exercise 2.25

Give combinations of cars and cdrs that will pick 7 from each of the following lists:

(1 3 (5 7) 9)
((7))
(1 (2 (3 (4 (5 (6 7))))))

纯看耐心。。

; 先创建这些要求的列表
(list 1 3 (list 5 7) 9)
(list (list 7))
(list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7))))))

; 根据它们的结构依次定位到 7
(car (cdr (car (cdr (cdr (list 1 3 (list 5 7) 9))))))
(car (car (list (list 7))))
(car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7))))))))))))))))))

; 执行结果
'(1 3 (5 7) 9)
'((7))
'(1 (2 (3 (4 (5 (6 7))))))
7
7
7

Exercise 2.26

Suppose we define x and y to be two lists:

(define x (list 1 2 3))
(define y (list 4 5 6))

What result is printed by the interpreter in response to evaluating each of the following expressions:

(append x y)
(cons x y)
(list x y)

结果如下所示:

'(1 2 3 4 5 6)
'((1 2 3) 4 5 6)
'((1 2 3) (4 5 6))

Exercise 2.27

Modify your reverse procedure of Exercise 2.18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example,

(define x (list (list 1 2) (list 3 4)))
x
((1 2) (3 4))
(reverse x)
((3 4) (1 2))
(deep-reverse x)
((4 3) (2 1))

这道题难度还是挺大的,我开始想用 reverse 实现,比起原来的 reverse 函数,deep-reverse 需要在 items 不为空的情况下,额外判断 (car items) 是否为 pair,虽然也能做,但是步骤有点麻烦,最后在网上发现一个使用 append 的版本,程序非常简单,而且逻辑也很清楚。

(define (deep-reverse-by-reverse items) 
  (define (iter items result) 
    (if (null? items) 
        result 
        (if (pair? (car items)) 
            (let ((x (iter (car items) nil))) 
              (iter (cdr items) (cons x result))) 
            (iter (cdr items) (cons (car items) result))))) 
  (iter items nil))

(define (deep-reverse items)
  (if (pair? items)
      (append (deep-reverse (cdr items))
              (list (deep-reverse (car items))))
      items))


(define x (list (list 1 2) (list 3 4)))

x
(reverse x)
(deep-reverse x)

标签:reverse,items,list,sicp,deep,cdr,car,2.27,2.24
From: https://www.cnblogs.com/think2times/p/18428613

相关文章

  • sicp每日一题[2.20]
    Exercise2.20Theprocedures+,*,andlisttakearbitrarynumbersofarguments.Onewaytodefinesuchproceduresistousedefinewithdotted-tailnotation.Inaproceduredefinition,aparameterlistthathasadotbeforethelastparameternameindic......
  • sicp每日一题[2.13-2.16]
    Exercise2.13Showthatundertheassumptionofsmallpercentagetolerancesthereisasimpleformulafortheapproximatepercentagetoleranceoftheproductoftwointervalsintermsofthetolerancesofthefactors.Youmaysimplifytheproblembyassu......
  • sicp每日一题[2.10]
    Exercise2.10BenBitdiddle,anexpertsystemsprogrammer,looksoverAlyssa’sshoulderandcommentsthatitisnotclearwhatitmeanstodividebyanintervalthatspanszero.ModifyAlyssa'scodetocheckforthisconditionandtosignalanerror......
  • sicp每日一题[2.10]
    Exercise2.11Inpassing,Benalsocrypticallycomments:“Bytestingthesignsoftheendpointsoftheintervals,itispossibletobreakmul-intervalintoninecases,onlyoneofwhichrequiresmorethantwomultiplications.”Rewritethisprocedureusin......
  • sicp每日一题[2.9]
    Exercise2.9Thewidthofanintervalishalfofthedifferencebetweenitsupperandlowerbounds.Thewidthisameasureoftheuncertaintyofthenumberspecifiedbytheinterval.Forsomearithmeticoperationsthewidthoftheresultofcombiningtwo......
  • sicp每日一题[2.8]
    Exercise2.8>UsingreasoninganalogoustoAlyssa's,describehowthedifferenceoftwointervalsmaybecomputed.Defineacorrespondingsubtractionprocedure,calledsub-interval.这道题目也比较简单,只要注意到区间之差的下界是被减区间的下界减去另一个区间的上......
  • sicp每日一题[2.7]
    2.7Alyssa’sprogramisincompletebecauseshehasnotspecifiedtheimplementationoftheintervalabstraction.Hereisadefinitionoftheintervalconstructor:(define(make-intervalab)(consab))Defineselectorsupper-boundandlower-boundtoco......
  • sicp每日一题[2.6]
    Exercise2.6Incaserepresentingpairsasprocedureswasn'tmind-bogglingenough,considerthat,inalanguagethatcanmanipulateprocedures,wecangetbywithoutnumbers(atleastinsofarasnonnegativeintegersareconcerned)byimplementing0a......
  • fastjson1.2.24反序列化漏洞复现 CVE-2017-18349
    1.准备:1.1复现环境漏洞环境:vulnhub靶场工具准备:jdk8,apache-maven-3.9.9,kali2024.1,MarshalSec1.2环境启动进入vulnhub目录下的fastjson目录,进入CVE-2017-18349目录cd/home/hbesljx/vulhub/fastjson/1.2.24-rcedocker-compoe启动漏洞环境docker-composeup-d访问靶机......
  • sicp每日一题[2.5]
    Exercise2.5Showthatwecanrepresentpairsofnonnegativeintegersusingonlynumbersandarithmeticoperationsifwerepresentthepairaandbastheintegerthatistheproduct2a3b.Givethecorrespondingdefinitionsoftheprocedurescons,car,a......