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 ...
随机推荐
- Dev 13.2 汉化教程(提供汉化cs文件下载)
主要为了汉化 FindPanel里的Find和Clear控件名称,研究了一些时间. 废话不说.直接上干货. 1.已将cs文件分享,下载放到项目里. 下载链接: http://pan.baidu.com ...
- linux用户和组管理,/etc/passwd 、/etc/shadow和/etc/group 文件内容解释
与用户相关的系统配置文件主要有/etc/passwd 和/etc/shadow,其中/etc/shadow是用户资讯的加密文件,比如用户的密码口令的加密保存等: /etc/passwd 和/etc/s ...
- require.js源码分析
写的寥寥草草,博客园的布局怎么弄还没有研究,再保存一份草稿,日后在完善,深度研究 require.js 加载顺序 1:加载html主页,require.js文件 2:脚本执行到html中的script ...
- Cairo 下载,测试
You need to download the all-in-one bundle available here. You can discover this link yourself by vi ...
- 第三方FMDB的简单使用
1,导入第三方头文件 #import "FMDB.h" //定义全局变量 @implementation InputInformationViewController { UITe ...
- sqlite学习
一鼓作气,今天继续学习了sqlite数据库在Xcode上的一些操作,主要是通过用oc代码进行salite表格的创建,删除,修改:以及对现有的表格数据进行增,删,改,查.虽然有点累,但是收获不小,感觉很 ...
- ubuntu安装谷歌输入法
1,sudo apt-get install fcitx-googlepinyin 2,在settings->Language Support里将keyboard input method sy ...
- RecyclerView因版本问题无法加载
前几天在学习RecyclerView时候,一直失败,各种加载不上.下面是错误信息 D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL ...
- iOS保存cookie的方法
SURLRequest*request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/ ...
- UE4蓝图编程的第一步
认识UE4蓝图中颜色与变量类型: UE4中各个颜色对应着不同的变量,连接点和连线的颜色都在表示此处是什么类型的变量.对于初学者来说一开始看到那么多连接点, 可能会很茫然,搞不清还怎么连,如果知道了颜色 ...