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

sicp每日一题[2.29]

时间:2024-09-26 12:14:40浏览次数:8  
标签:right weight mobile 每日 sicp 2.29 branch make define

Exercise 2.29

A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list):

(define (make-mobile left right)
  (list left right))

A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile:

(define (make-branch length structure)
  (list length structure))

a. Write the corresponding selectors left-branch and right-branch, which return the branches of a mobile, and branch-length and branch-structure, which return the components of a branch.
b. Using your selectors, define a procedure total-weight that returns the total weight of a mobile.
c. A mobile is said to be balanced if the torque applied by its top-left branch is equal to that applied by its top right branch (that is, if the length of the le rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging offits branches is balanced. Design a predicate that tests whether a binary mobile is balanced.
d. Suppose we change the representation of mobiles so that the constructors are

(define (make-mobile left right) (cons left right))
(define (make-branch length structure) 
  (cons length structure))

How much do you need to change your programs to convert to the new representation?


这道题主要是需要理解 mobile 是什么东西,我感觉它跟杠杆很像,然后就比较好处理了。

; a
(define (left-branch mobile)
  (car mobile))

(define (right-branch mobile)
  (car (cdr mobile)))

(define (branch-length branch)
  (car branch))

(define (branch-structure branch)
  (car (cdr branch)))

; b
(define (total-weight mobile)
  (cond ((null? mobile) 0)
        ((not (pair? mobile)) mobile)
        (else (+ (total-weight (branch-structure (left-branch mobile)))
                 (total-weight (branch-structure (right-branch mobile)))))))

; c
(define (torque branch)
  (* (branch-length branch)
     (total-weight (branch-structure branch))))

(define (balanced? mobile)
  (if (not (pair? mobile))
      true
      (= (torque (left-branch mobile))
         (torque (right-branch mobile)))))

(newline)
(define a (make-mobile (make-branch 2 3) (make-branch 3 2)))
(total-weight a)
(balanced? a)

(newline)
(define b (make-mobile (make-branch 2 a) (make-branch 4 a)))
(total-weight b)
(balanced? b)

(newline)
(define c (make-mobile (make-branch 2 b) (make-branch 3 a)))
(total-weight c)
(balanced? c)

; 执行结果
5
#t

10
#f

15
#f

; d
(define (right-branch mobile)
  (cdr mobile))

(define (branch-structure branch)
  (cdr branch))

标签:right,weight,mobile,每日,sicp,2.29,branch,make,define
From: https://www.cnblogs.com/think2times/p/18433189

相关文章

  • 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)......
  • Day5 JavaWeb知识了解以及每日一题:力扣125.验证回文串
    Day5JavaWeb知识了解以及每日一题:力扣125.验证回文串2024年9月24日20:06:45JavaWeb基础知识TomcatApacheTomcat是一个开源的Servlet容器和Web服务器,它是JavaEE(EnterpriseEdition)的一部分,专门用于运行JavaServlet和JavaServerPages(JSP)。Tomcat的主要功能是接收HTTP......
  • SolidJS-每日小知识(9/24)
    对图片指定范围的区域进行填充显示1定义变量,svg和image//用于保存SVG元素的引用const[svgRef,setSvgRef]=createSignal<SVGSVGElement|null>(null);//图像原始尺寸constimageSize={width:11920,height:16850};//裁剪区域constcroppedScope......
  • 软设每日打卡——在一个页式存储管理系统中,页表内容如下所示: 若页的大小为4KB,则地址转
    在一个页式存储管理系统中,页表内容如下所示:                  若页的大小为4KB,则地址转换机构将逻辑地址0转换成物理地址(块号在0开始计算)为A、8192        A、4096        C、2048        D、1024        答案:A解:......
  • sicp每日一题[2.24-2.27]
    2.24-2.26没什么代码量,所以跟2.27一起发吧。Exercise2.24Supposeweevaluatetheexpression(list1(list2(list34))).Givetheresultprintedbytheinterpreter,thecorrespondingbox-and-pointerstructure,andtheinterpretationofthisasatree(as......
  • Java基础练习(每日五题)
    1,通过代码编写,输一段话:“今天是学习的第一天”packagejava4;publicclasspractise{publicstaticvoidmain(String[]args){System.out.println("今天是学习的第一天");}}2,拼接打印:“XXX:我已经学习了JavaX年,我期望的工资是XXX”packagejava4;......
  • 苍穹外卖学习日志 -----20天项目从零到完结-----含软件下载,环境配置,框架学习,代码编写,
    年份2024    基础:Javase  Javaweb已完结   2024  8.25---9.14  20天Day-01   8.25今天开始学习已经晚了,网盘下载了一下文件,做了一些开始项目的准备工作。本来其实打算用notepad++来写学习日志的,但是那个传不了图片,而且编辑视图没有这......
  • 9.18每日总结
    今日学习时间一小时,echarts成功连接到了后天数据库,完成了实时动态表格,但是没并灭有使用ajax的方法,而是通过获取数据,之后进行字符串拼接的方式完成了获取数据库数据<%List<User>userList=(List<User>)session.getAttribute("u");StringBuilderuserIds=newStri......
  • py每日spider案例之网站视频接口
    importrequestscookies={'auth_id':'eyJpdiI6IlUzOEVzajFocW1ydGh4TGE0R00yaXc9PSIsInZhbHVlIjoidmw2UWF0cFJBMGF0TStBM0dBWVFNN09lMFpMV2xlMHdJSG1Ma1g4TUtSV0loKzJEY1psKzVML0ZjeVJUK1BTbk1obkFpYWNMUXdLSTJXWjdOK2lZSFluL3A4WmxkVDNoUElHbGx5UG9......