首页 > 其他分享 >sicp每日一题[2.38-2.39]

sicp每日一题[2.38-2.39]

时间:2024-10-09 09:02:34浏览次数:1  
标签:right nil sequence 2.39 list sicp 2.38 fold left

Exercise 2.38

The accumulate procedure is also known as fold-right, because it combines the first element of the sequence with the result of combining all the elements to the right. There is also a fold-left, which is similar to fold-right, except that it combines elements working in the opposite direction:

(define (fold-left op initial sequence)
  (define (iter result rest)
    (if (null? rest)
        result
        (iter (op result (car rest))
              (cdr rest))))
  (iter initial sequence))

What are the values of

(fold-right / 1 (list 1 2 3))
(fold-left / 1 (list 1 2 3))
(fold-right list nil (list 1 2 3))
(fold-left list nil (list 1 2 3))

Give a property that op should satisfy to guarantee that fold-right and fold-left will produce the same values for any sequence.


这道题还是挺简单的,题目给的2组例子都是结果不一样的,所以很容易就会想到跟顺序无关的操作,比如加法和乘法。

(fold-right / 1 (list 1 2 3))
(fold-left / 1 (list 1 2 3))

(fold-right list nil (list 1 2 3))
(fold-left list nil (list 1 2 3))


(fold-right + 1 (list 1 2 3))
(fold-left + 1 (list 1 2 3))

(fold-right * 1 (list 1 2 3))
(fold-left * 1 (list 1 2 3))

; 执行结果
1 1/2
1/6
'(1 (2 (3 ())))
'(((() 1) 2) 3)
7
7
6
6

Exercise2.39

Complete the following definitions of reverse (Exercise 2.18) in terms of fold-right and fold-left from Exercise 2.38:

(define (reverse sequence)
  (fold-right (lambda (x y) ⟨??⟩) nil sequence))

(define (reverse sequence)
  (fold-left (lambda (x y) ⟨??⟩) nil sequence))

这道题还是有难度的,我只做出了用 fold-left 实现的部分,用 fold-right 实现的时候,开始用的 cons 和 list,顺序虽然是对的,但是多了很多括号,最后我也想到了使用 append,但是一直报错,我试了一下没有找到解决办法,最后去搜了一下别人的答案。。

(define (reverse-by-right sequence)
  (fold-right (lambda (x y) (append y (list x))) nil sequence))

(define (reverse-by-left sequence)
  (fold-left (lambda (x y) (cons y x)) nil sequence))

(define odds (list 1 3 5 7 9))
(reverse-by-right odds)
(reverse-by-left odds)

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

标签:right,nil,sequence,2.39,list,sicp,2.38,fold,left
From: https://www.cnblogs.com/think2times/p/18453464

相关文章

  • sicp每日一题[2.36-2.37]
    果然习惯不能停,就两天没学,昨天就忘的干干净净了。。今天把昨天的补上Exercise2.36Theprocedureaccumulate-nissimilartoaccumulateexceptthatittakesasitsthirdargumentasequenceofsequences,whichareallassumedtohavethesamenumberofelements......
  • sicp每日一题[2.34]
    Exercise2.34Evaluatingapolynomialinxatagivenvalueofxcanbeformulatedasanaccumulation.Weevaluatethepolynomialanx^n+a{n-1}x^(n-1)+...+a1x+a0usingawell-knownalgorithmcalledHorner'srule,whichstructuresthecom......
  • sicp每日一题[2.33]
    Exercise2.33Fillinthemissingexpressionstocompletethefollowingdefinitionsofsomebasiclist-manipulationoperationsasaccumulations:;p表示一个函数,sequence表示一个列表;这个函数将对列表中每一个元素进行p操作(define(mappsequence)(accu......
  • sicp每日一题[2.31]
    Exercise2.31AbstractyouranswertoExercise2.30toproduceaprocedure$tree-map$withthepropertythat$square-tree$couldbedefinedas(define(square-treetree)(tree-mapsquaretree))这道题跟上面一道的map实现几乎一模一样,我还以为我理解错题目......
  • sicp每日一题[2.32]
    上一道题没什么改动,再来一道Exercise2.32Wecanrepresentasetasalistofdistinctelements,andwecanrepresentthesetofallsubsetsofthesetasalistoflists.Forexample,ifthesetis(123),thenthesetofallsubsetsis(()(3)(2)(23)......
  • sicp每日一题[2.29]
    Exercise2.29Abinarymobileconsistsoftwobranches,aleftbranchandarightbranch.Eachbranchisarodofacertainlength,fromwhichhangseitheraweightoranotherbinarymobile.Wecanrepresentabinarymobileusingcompounddatabyconstru......
  • sicp每日一题[2.28]
    Exercise2.28Writeaprocedurefringethattakesasargumentatree(representedasalist)andreturnsalistwhoseelementsarealltheleavesofthetreearrangedinleft-to-rightorder.Forexample,(definex(list(list12)(list34)))(fringex)......
  • sicp每日一题[2.24-2.27]
    2.24-2.26没什么代码量,所以跟2.27一起发吧。Exercise2.24Supposeweevaluatetheexpression(list1(list2(list34))).Givetheresultprintedbytheinterpreter,thecorrespondingbox-and-pointerstructure,andtheinterpretationofthisasatree(as......
  • sicp每日一题[2.20]
    Exercise2.20Theprocedures+,*,andlisttakearbitrarynumbersofarguments.Onewaytodefinesuchproceduresistousedefinewithdotted-tailnotation.Inaproceduredefinition,aparameterlistthathasadotbeforethelastparameternameindic......
  • sicp每日一题[2.13-2.16]
    Exercise2.13Showthatundertheassumptionofsmallpercentagetolerancesthereisasimpleformulafortheapproximatepercentagetoleranceoftheproductoftwointervalsintermsofthetolerancesofthefactors.Youmaysimplifytheproblembyassu......