some simple recursive lisp programs
1. Write a procedure count-list to count the number of elements in a list
(defun count-list (numbers)
(
(+ (count-list (rest numbers)))))
(print (count-list '(1 2 3)))5 6 result: 3
2. Write a procedure reverse-list to reverse each word in a list of words
(defun reverse-list (numbers)
(if (null numbers) nil
(cons (reverse (first numbers)) (reverse-list (rest numbers)))))
(reverse-list '("dog" "pan" "tar" "tip" "net"))
result: ("god" "nap" "rat" "pit" "ten")
3. Write a procedure evenp-list to process a list of numbers, replacing each number by t if it's even, and nil if it's odd
(defun evenp-list (numbers)
(if (null numbers) nil
(cons (if (evenp (first numbers)) t nil)
(evenp-list (rest numbers)))))
(evenp-list '(1 2 3 4 5 6 7 8))
result:
(nil t nil t nil t nil t)
4. Write a procedure max-list to return the maximum element of a list.
(defun max-list (numbers)
(
(if (> (first numbers) (max-list (rest numbers)))
(first numbers)
(max-list (rest numbers)))))
(max-list '(11 13 17 19 2 3 5 7))
should return 19.
These three small programs are all recursive. It is interesting to code in lisp. ;)
The fourth program is incorrect. There is bug: I assume that all numbers in the list are positive.
The following program is better:
(defun max-list (numbers)
(if (null (rest numbers)) (first numbers)
(if (> (first numbers) (max-list (rest numbers)))
(first numbers)
(max-list (rest numbers)))))
btw, max is a build-in procedure. so:
(defun max-list (numbers)
(if (null (rest numbers)) (first numbers)
(max (first numbers) (max-list (rest numbers))
)))
It becomes simpler. ;)
some simple recursive lisp programs的更多相关文章
- scheme和common lisp 区别
Scheme and Common Lisp use different names for some of the basic system functions. Many Lisp program ...
- 10994 - Simple Addition(规律)
Problem E Simple Addition Input: Standard Input Output: Standard Output Let’s define a simple recurs ...
- Bloomberg面经准备: Josephus problem
Given a circular single linked list.Write a program that deletes every kth node until only one node ...
- java源码剖析: 对象内存布局、JVM锁以及优化
一.目录 1.启蒙知识预热:CAS原理+JVM对象头内存存储结构 2.JVM中锁优化:锁粗化.锁消除.偏向锁.轻量级锁.自旋锁. 3.总结:偏向锁.轻量级锁,重量级锁的优缺点. 二.启蒙知识预热 开启 ...
- Why Doesn't Python Have Switch/Case?
Why Doesn't Python Have Switch/Case? Tuesday, June 09, 2015 (permalink) Unlike every other programmi ...
- Dynamic Programming | Set 1 (Overlapping Subproblems Property)
动态规划是这样一种算法范式:将复杂问题划分为子问题来求解,并且将子问题的结果保存下来以避免重复计算.如果一个问题拥有以下两种性质,则建议使用动态规划来求解. 1 重叠子问题(Overlapping S ...
- [Optimization] Advanced Dynamic programming
这里主要是较为详细地理解动态规划的思想,思考一些高质量的案例,同时也响应如下这么一句口号: “迭代(regression)是人,递归(recursion)是神!” Video series for D ...
- jdk源码剖析二: 对象内存布局、synchronized终极原理
很多人一提到锁,自然第一个想到了synchronized,但一直不懂源码实现,现特地追踪到C++层来剥开synchronized的面纱. 网上的很多描述大都不全,让人看了不够爽,看完本章,你将彻底了解 ...
- ElasticSearch 2 (5) - Document APIs
ElasticSearch 2.1.1 (5) - Document APIs This section describes the following CRUD APIs: Single docu ...
随机推荐
- LINUX:read、array、declare
read:要读取来自键盘输入的变量 使用规则: read [-pt] variale 选项与参数: -p:后面接提示字符: -t:后面接等待的“秒数”: 如果read之后不加任何参数,直接加上变量名称 ...
- AngularJS提供的内置过滤器
1. currencycurrecy过滤器可以将一个数值格式化为货币格式.用{{ 123 | currency }}来将123转化成货币格式.currecy过滤器允许我们自己设置货币符号.默认情况下会 ...
- 初探canvas
canvas是html5新增的一个专用于图形处理的标签,利用canvas可以实现大部分图形操作canvas的一些基本操作与其他图形编程工具类似,包含:各种形状的边框.路径绘制和填充,画布属性调整,样式 ...
- error the @annotation pointcut expression is only supported at Java 5 compliance
今天碰到这个错误,在网上找了下,是因为aspectjweaver.jar用的是1.5.3 本地eclipse的jdk版本为1.7 下载高版本的aspectjweaver.jar会解决此问题 http: ...
- psp个人软件开发
为开发人员提供一个PSP工具,简化时间记录工作:同时提供数据使用的工具,帮助开发人提高估算能力. 需求分析: 编号 特性 FEAT01 研发经理能够创建项目.指定或修改项目经理.删除尚未分配工作任务 ...
- 杭电ACM1003
原题: Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Android手机无法访问百度空间的解决办法
本文网址:http://www.cnblogs.com/tunnel213/p/4301165.html 现象: 百度“JavaScript函数高级”后找到一篇文章,百度空间的,无法查看: 配置: 三 ...
- 开启GZIP(转)
因为在做一个项目,项目里面服务器主要提供数据,但是数据多了文件就大了,比较浪费流量和时间,我们便用Gzip来处理.我在本机上是apache,服务器上是IIS6.0,用的是php,那么我就在这里分享一下 ...
- PCB Layout高速电路设计小知识点
1.单位:1mil = 0.0254mm 2.default线宽及线距(综合考虑高速电路性能及PCB板厂制程能力):6mil 3.差分走线特征阻抗:100ohm 4.3W原则,即线间距采用3倍线宽,多 ...
- 366. Find Leaves of Binary Tree
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...