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 ...
随机推荐
- JSP内置对象---用户登录页面(get和post)
Login.jsp 页面: <%@ page language="java" import="java.util.*" contentType=" ...
- json处理总结(前端js和后端java)
前端(js): json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键,下面将介绍两者之间的相互转换. json字符串:var st ...
- C# dll加载,抽象方法的使用
抽象类! dll的使用 /// <summary> /// 返回类型--插件 /// </summary> /// <param name="baseName& ...
- J2EE、J2SE、J2ME是什么意思?
本文介绍Java的三大块:J2EE.J2SE和J2ME.J2SE就是Java2的标准版,主要用于桌面应用软件的编程:J2ME主要应用于嵌入是系统开发,如手机和PDA的编程:J2EE是Java2的企业版 ...
- Oracle开窗函数 over()(转)
copy文链接:http://blog.csdn.net/yjjm1990/article/details/7524167#,http://www.2cto.com/database/201402/2 ...
- 【1】springmvc4 + servlet3 零配置全注解入门项目helloword
自从servlet3.0支持注解,使得javaweb项目使用全注解成为可能. 注解化将成为javaweb的发展方向.包括spring去年推出的spring-boot也是全部使用注解. 代码:https ...
- SharePoint Framework 配置Office 365开发者租户
博客地址:http://blog.csdn.net/FoxDave 你需要一个Office 365开发者租户来使用预览版SharePoint Framework构建和发布客户端web部件.你的租户 ...
- ADO.NET与ORM的比较:NHibernate实现CRUD(转)
原文地址 http://blog.csdn.net/zhoufoxcn/article/details/5402511 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spr ...
- dedecms购物车商品添加删除数量改变方式变成ajax
简单的做了一下修改,模板用的它默认的模板,感觉之前全是表单提交很不爽用的 修改的相关文件:/plus/posttocar.php, /plus/car.php,/templets/plus/car.h ...
- mysql 获取当前日期及格式化 (转)
MYSQL 获取当前日期及日期格式获取系统日期: NOW()格式化日期: DATE_FORMAT(date, format)注: date:时间字段format:日期格式 返回系统日期,输出 2009 ...