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

sicp每日一题[2.2]

时间:2024-09-08 09:03:08浏览次数:11  
标签:end point 每日 sicp start 2.2 segment make define

Exercise 2.2

Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints). To try your procedures, you’ll need a way to print points:

(define (print-point p)
  (newline)
  (display "(")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))

这道题目很简单,主要是为了检查我们是否理解了嵌套使用抽象数据结构的概念,有点像 Python 里的列表,每个列表元素也可以是列表。

(define (make-point x y)
  (cons x y))

(define (x-point p) (car p))

(define (y-point p) (cdr p))

(define (make-segment start end)
  (cons start end))

(define (start-segment segment)
  (car segment))

(define (end-segment segment)
  (cdr segment))

; 线段中点坐标就是起点和终点横纵坐标的平均值
(define (midpoint-segment segment)
  (let ((start (start-segment segment))
        (end (end-segment segment)))
    (make-point (average (x-point start) (x-point end))
                (average (y-point start) (y-point end)))))


; 设置线段起点为(3, 5),终点为(7, 7),中点应该为(5, 6)
(define start (make-point 3 5))
(define end (make-point 7 7))
(define line (make-segment start end))
(print-point (midpoint-segment line))

; 执行结果
(5, 6)

标签:end,point,每日,sicp,start,2.2,segment,make,define
From: https://www.cnblogs.com/think2times/p/18402552

相关文章

  • 每日OJ_牛客_骆驼命名法(递归深搜)
    目录牛客_骆驼命名法(简单模拟)解析代码牛客_骆驼命名法(简单模拟)骆驼命名法__牛客网解析代码首先一个字符一个字符的读取内容:遇到_就直接跳过。如果上一个字符是_则下一个字符转大写字母。#include<iostream>#include<string>usingnamespacestd;intmai......
  • GitHub每日最火火火项目(9.6)
    项目名称:Zeyi-Lin/HivisionIDPhotos项目介绍:HivisionIDPhotos是一个轻量级且高效的AI证件照制作工具。它能够通过人工智能技术,快速、准确地生成符合要求的证件照。这个工具具有较高的实用性,可以帮助用户节省时间和精力,无需专业的摄影知识和技能,就能轻松制作出满意的......
  • 【每日刷题】Day112
    【每日刷题】Day112......
  • java计算机毕业设计每日一课微党课学习管理平台(开题+程序+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景在信息化高速发展的今天,党员教育工作面临着新的机遇与挑战。传统的党员学习模式往往受限于时间、空间及资源,难以适应新时代党员教育高效化、个性化的......
  • 必应每日壁纸API封装
    简介这个类封装了必应首页的每日壁纸查看功能,提供了查看、保存壁纸的方法,最大支持查看近8天的壁纸使用方法asyncTaskMain(){ try { varbing=BingWallpaperAPI.CreateInstance(8);//初始化,参数8表示一共会加载8张图片 vartask=awaitbing.Current(); task.Wal......
  • 【每日一练】tkinter输入文本框实例
    """代码思路1.先创建一个文本框2.定义一个函数执行文本的获取和另一个文本框的赋值3.创建按钮,并调用函数4.创建另一个文本框"""importtkinterastkroot=tk.Tk()root.title("复制输入框")root.geometry("400x300+800+200")#创建一个输入文本框,显示为*的属性设......
  • Oracle 12.2.0.1.0单实例安装补丁
    ......
  • 2.2 画出函数图形
    点击查看代码importsympyasspimportnumpyasnpimportmatplotlib.pyplotasplt#定义符号t,x=sp.symbols('tx')#计算不定积分integral=sp.integrate(sp.exp(-t)*t**(x-1),t)#选择一个x的值进行绘图(例如,x=2)x_value=2inte......
  • 软设每日打卡——霍夫曼编码将频繁出现的字符釆用短编码,出现频率较低的字符采用长编码
    【题目】霍夫曼编码将频繁出现的字符釆用短编码,出现频率较低的字符采用长编码。具体        的操作过程为:i)以每个字符的出现频率作为关键字构建最小优先级队列;ii)取出关键        字最小的两个结点生成子树,根节点的关键字为孩子节点关键字之和,并将根节点......
  • sicp每日一题[1.46]
    Exercise1.46Severalofthenumericalmethodsdescribedinthischapterareinstancesofanextremelygeneralcomputationalstrategyknownasiterativeimprovement.Iterativeimprovementsaysthattocomputesomething,westartwithaninitialguessfor......