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 ...
随机推荐
- spring通过静态方法获得properties文件的值
获得spring bean方法 @Component public class BeanUtils implements ApplicationContextAware { private stati ...
- Spring IOC 依赖注入的两种方式XML和注解
依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...
- Tomcat Shell脚本(启动|关闭|重启|状态)
#!/bin/bash # # chkconfig: - # description: Tomcat start/stop/status script #Location of JAVA_HOME ( ...
- Unreal Engine Plugin management
Be aware to remove any dependencies to any modules related to Editor, or else it will end up with fa ...
- Spring学习笔记(一)
1.1.1Spring是什么? Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发. 1.1.2S ...
- 动态生成tr,并将其下控件的值拼接后传到后台并保存
有两个表(主表和子表),现在需要根据主表某一个字段动态的生成记录(一条记录就一个tr),然后再讲tr下控件的各个值取出来,传到后台,并保存到子表. html代码: <!--#for(Record ...
- NOIP 考前 KMP练习
BZOJ 1461 && BZOJ 1729 KMP+BIT 一看就是字符串匹配但是不同的是要按照每个字符的排名情况. 首先对于数字x的排名,那么要判断x前小于x的数的个数,和x前小于 ...
- meta头部标签意义作用!
1.<meta name="viewport" id="viewport" content="width=device-width, initi ...
- DataGridview 自动切换到 下一行
if (m_dgvList.SelectedRows.Count > 0) { int intCurrApp = m_dgvList.SelectedRows[0].Index; if (int ...
- eclipse快捷键以及使用技巧大全
eclipse快捷键以及使用技巧大全1. 打开MyEclipse 6.0.1,然后"window"→"Preferences" 2. 选择"java& ...