首页 > 其他分享 >sicp每日一题[2.31]

sicp每日一题[2.31]

时间:2024-09-28 08:54:48浏览次数:1  
标签:map square 2.31 sub list 每日 tree sicp

Exercise 2.31

Abstract your answer to Exercise 2.30 to produce a procedure $tree-map$ with the property that $square-tree$ could be defined as

(define (square-tree tree) (tree-map square tree))

这道题跟上面一道的 map 实现几乎一模一样,我还以为我理解错题目了,上网搜了一下,发现别人也是这么写的,那就这样吧。

(define (tree-map f tree)
  (map (lambda (sub-tree)
         (if (pair? sub-tree)
             (tree-map f sub-tree)
             (f sub-tree)))
       tree))

(define (square-tree tree) (tree-map square tree))


(square-tree
 (list 1
       (list 2 (list 3 4) 5)
       (list 6 7)))

; 执行结果
'(1 (4 (9 16) 25) (36 49))

标签:map,square,2.31,sub,list,每日,tree,sicp
From: https://www.cnblogs.com/think2times/p/18436956

相关文章

  • sicp每日一题[2.32]
    上一道题没什么改动,再来一道Exercise2.32Wecanrepresentasetasalistofdistinctelements,andwecanrepresentthesetofallsubsetsofthesetasalistoflists.Forexample,ifthesetis(123),thenthesetofallsubsetsis(()(3)(2)(23)......
  • GitHub每日最火火火项目(9.27)
    项目名称:localsend/localsend项目介绍:“localsend/localsend”是一个极具价值的开源项目。它为用户提供了一种跨平台的文件传输替代方案,可媲美AirDrop。在当今数字化时代,人们常常需要在不同操作系统的设备之间传输文件,但并非所有设备都能使用AirDrop。这个项目的出......
  • 9.24每日总结
    微服务拆分作业参考依赖user-service的pom.xml文件内容如下:<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xs......
  • 9.26每日总结
    给出SpringbootCloud的server:port:8084spring:application:name:user-serviceprofiles:active:devdatasource:url:jdbc:mysql://${hm.db.host}:3306/hm-user?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=......
  • 9.25每日总结 OpenFeign
    OpenFeign利用Nacos实现了服务的治理,利用RestTemplate实现了服务的远程调用。但是远程调用的代码太复杂了:而且这种调用方式,与原本的本地方法调用差异太大,编程时的体验也不统一,一会儿远程调用,一会儿本地调用。用到OpenFeign组件了。其实远程调用的关键点就在于四个:请求方式......
  • 洛谷每日一题(P1540 [NOIP2010 提高组] 机器翻译)
    原题目链接:P1540[NOIP2010提高组]机器翻译-洛谷|计算机科学教育新生态(luogu.com.cn)原题目截图:思路分析:读懂题意,直接模拟过程即可。这是一道很简单的题目。思路过程很类似模拟页面置换算法中的先进先出(FIFO)策略。因此我们很容易想到,要用队列来实现。下面是......
  • leetcode每日一题day15(24.9.25)——公司命名
    思路:首先如果没有相同的后缀,则无论只要不是相同的首字母交换都不会出现重复情况,如果有重复后缀,则还需多增加个不能和,首字符与另一相同后缀字串的首字符相同的字串交换。主要矛盾已经明确,则可对矛盾进行分析。首先把范围缩小到只有两种不同首字母,对于这种情况      ......
  • sicp每日一题[2.29]
    Exercise2.29Abinarymobileconsistsoftwobranches,aleftbranchandarightbranch.Eachbranchisarodofacertainlength,fromwhichhangseitheraweightoranotherbinarymobile.Wecanrepresentabinarymobileusingcompounddatabyconstru......
  • 262.行程与用户(如何计算每日/3日滚动/5日滚动订单取消率
    1.题目你需要编写一个SQL解决方案,计算2013-10-01至2013-10-03期间,非禁止用户的取消率。非禁止用户是指banned字段为'No'的用户。取消率的计算需要满足以下条件:乘客和司机都必须未被禁止,即他们的banned字段值均为'No'。取消率的计算公式为:取消率 (Cancellation......
  • sicp每日一题[2.28]
    Exercise2.28Writeaprocedurefringethattakesasargumentatree(representedasalist)andreturnsalistwhoseelementsarealltheleavesofthetreearrangedinleft-to-rightorder.Forexample,(definex(list(list12)(list34)))(fringex)......