当前位置: 首页 > news >正文

sicp每日一题[2.20]

Exercise 2.20

The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use d e f i n e define define with d o t t e d − t a i l n o t a t i o n dotted-tail notation dottedtailnotation. In a procedure definition, a parameter list that has a dot before the last parameter name indicates that, when the procedure is called, the initial parameters (if any) will have as values the initial arguments, as usual, but the final parameter’s value will be a list of any remaining arguments. For instance, given the definition
(define (f x y . z) ⟨body⟩)
the procedure f can be called with two or more arguments. If we evaluate
(f 1 2 3 4 5 6)
then in the body of f, x will be 1, y will be 2, and z will be the list (3 4 5 6). Given the definition
(define (g . w) ⟨body⟩)
the procedure g can be called with zero or more arguments. If we evaluate
(g 1 2 3 4 5 6)
then in the body of g, w will be the list (1 2 3 4 5 6).
Use this notation to write a procedure s a m e − p a r i t y same-parity sameparity that takes one or more integers and returns a list of all the arguments that have the same even-odd parity as the first argument. For example,

(same-parity 1 2 3 4 5 6 7)
(1 3 5 7)
(same-parity 2 3 4 5 6 7)
(2 4 6)

这道题也是看着唬人,题干挺长,但是主要在介绍如何定义参数个数不定的函数,真正的题目就最后一段,就是要根据第一个参数的奇偶性,把后面参数中跟第一个参数奇偶性相同的找出来。

(define (same-parity x . y)(define (iter items r result)(cond ((null? items) result)((= r (remainder (car items) 2)) (iter (cdr items) r (cons (car items) result)))(else (iter (cdr items) r result))))(let ((r (remainder x 2)))(reverse (iter y r (cons x nil)))))(same-parity 1 2 3 4 5 6 7)
(same-parity 2 3 4 5 6 7)
(same-parity 2); 执行结果
'(1 3 5 7)
'(2 4 6)
'(2)

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • MySQL基础基础篇 - SQL
  • 数据结构:内部排序
  • spark 面试题
  • Linux 防火墙:iptables (二)
  • JVM 一个对象是否已经死亡?
  • ubuntu64位系统无法运行32位程序的解决办法
  • 前后端分离,使用MOCK进行数据模拟开发,让前端攻城师独立于后端进行开发
  • 【HTTP】构造HTTP请求和状态码
  • 大数据新视界 --大数据大厂之AI 与大数据的融合:开创智能未来的新篇章
  • C++——模板初阶
  • 后台数据管理系统 - 项目架构设计-Vue3+axios+Element-plus(0920)
  • 十五、差分输入运算放大电路
  • 前端web端项目运行的时候没有ip访问地址
  • 【秋招笔试-支持在线评测】8.28华为秋招(已改编)-三语言题解
  • 20240921在友善之臂的NanoPC-T6开发板上确认宸芯的数传模块CX6602N的AT命令
  • [译] 怎样写一个基础的编译器
  • css选择器
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • JS+CSS实现数字滚动
  • js如何打印object对象
  • js数组之filter
  • MD5加密原理解析及OC版原理实现
  • Swift 中的尾递归和蹦床
  • Terraform入门 - 1. 安装Terraform
  • 笨办法学C 练习34:动态数组
  • 大型网站性能监测、分析与优化常见问题QA
  • 互联网大裁员:Java程序员失工作,焉知不能进ali?
  • 简单基于spring的redis配置(单机和集群模式)
  • 世界上最简单的无等待算法(getAndIncrement)
  • 优秀架构师必须掌握的架构思维
  • ​io --- 处理流的核心工具​
  • ​Redis 实现计数器和限速器的
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • # 利刃出鞘_Tomcat 核心原理解析(八)-- Tomcat 集群
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (12)Hive调优——count distinct去重优化
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (Matlab)使用竞争神经网络实现数据聚类
  • (pytorch进阶之路)扩散概率模型
  • (补充):java各种进制、原码、反码、补码和文本、图像、音频在计算机中的存储方式
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (离散数学)逻辑连接词
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • (原創) 物件導向與老子思想 (OO)
  • (转)shell调试方法
  • ./configure、make、make install 命令
  • .dat文件写入byte类型数组_用Python从Abaqus导出txt、dat数据
  • .form文件_SSM框架文件上传篇
  • .NET Core 2.1路线图
  • .net MVC中使用angularJs刷新页面数据列表
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈
  • .NET+WPF 桌面快速启动工具 GeekDesk