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

sicp每日一题[2.20]

时间:2024-09-21 11:35:12浏览次数:1  
标签:parity items 每日 list sicp same will 2.20 define

Exercise 2.20

The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use define with dotted-tail notation. 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 same-parity 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)

标签:parity,items,每日,list,sicp,same,will,2.20,define
From: https://www.cnblogs.com/think2times/p/18423778

相关文章

  • 每日一问 | 黑猩猩算法
    1.黑猩猩算法是什么黑猩猩算法(ChimpOptimizationAlgorithm,COA)是一种基于黑猩猩群体行为的智能优化算法,属于群体智能算法的一种。这类算法通过模拟自然界中动物的行为模式,来解决复杂的优化问题。黑猩猩算法尤其模仿了黑猩猩在捕猎、交流、决策等方面的策略和行为,以实现全局和局......
  • 每日一题:Leetcode-347 前K个高频元素
    力扣题目解题思路java代码力扣题目:给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。示例1:输入:nums=[1,1,1,2,2,3],k=2输出:[1,2]示例2:输入:nums=[1],k=1输出:[1]提示:1<=nu......
  • 每日总结
    最近也是搞hadoop,hbase,zookeeper,phoenix,这四个东西,到处配置也是够累了,主要是网上的资料不是很详细,都有所不同,也不知道怎么去配置。就只能看很多的文章去筛选,但是差的就是细节,比如Linux的操作命令,有些变量不知道是不是要改成自己的。还有软件的版本适配的问题,教程都是几年前......
  • 每日分享之-MySQL单表
    1.什么是数据库?数据库:DataBase(DB),是存储和管理数据的仓库,本质就是一个存放数据的文件系统数据库会按照特定的格式对数据进行存储,用户可以对数据库中的数据进行增加,修改,删除及查询操作数据库管理系统:DataBaseManagementSystem (DBMS),操纵和管理数据库的大型软件。数据......
  • 【每日刷题】Day124
    【每日刷题】Day124......
  • 【每日刷题】Day125
    【每日刷题】Day125......
  • 算法学习每日一题之2332. 坐上公交的最晚时间:二分答案 & 贪心双指针
    Problem:2332.坐上公交的最晚时间人话题意:你是一个懒惰的人,虽然你要赶公交车,但你想多睡会,恰好你知道每辆车的发车时间buses和每辆车容capacity,和每个乘客乘车的时间passenger,旨在求可以赶上公交车的最晚出发时间。思路一:二分答案求最晚能满足赶上公交的时间,可以发现......
  • 【每日一题】LeetCode 2332.坐上公交的最晚时间(数组、双指针、二分查找、排序)
    【每日一题】LeetCode2332.坐上公交的最晚时间(数组、双指针、二分查找、排序)题目描述给你一个下标从0开始长度为n的整数数组buses,其中buses[i]表示第i辆公交车的出发时间。同时给你一个下标从0开始长度为m的整数数组passengers,其中passengers[j]表示第......
  • 【计算机网络 - 基础问题】每日 3 题(十一)
    ✍个人博客:Pandaconda-CSDN博客......
  • 【计算机网络 - 基础问题】每日 3 题(十二)
    ✍个人博客:Pandaconda-CSDN博客......