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 ...
随机推荐
- MicroERP开发技术分享:技术选型
为什么要想起开发一个近似一套完整的ERP软件呢,原因有二:一是想在空闲时间把以前的进销存软件丰富一下,结果越搞越大了:二是这些年光搞C#了,不想把VB6忘光了 非微软的东西还真没时间去学,也有主要原因 ...
- 【初级】linux mv 命令详解及使用方法实战
mv:移动文件或者将文件改名 前言: mv是move的缩写,顾名思义是移动.它的功能既能移动文件/文件夹,又可以用来改名,经常用来做文件的备份,比如再删除之前,先给文件做备份(保护数据)也是linux ...
- 黑马程序员——OC基础 三种特性之封装
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) 三种特性之一封装 (一)set方法和get方法 1) set方法 1&g ...
- eclipse 导入工程报错Unable to execute dex: Multiple dex files define Landroid/annotation/SuppressLint
对策: 检查libs 是否有重复加载的.
- [小菜随笔]关于monkey报错日志分析
今天小菜在一个测试群内看到群友发出一个monkey的报错信息,其实是一个很简单的报错 个人觉得monkey虽然操作起来比较简易,但其实查看日志分析日志也是很重要的环节,如果对错误分析不够详细,就容易误 ...
- nginx+tomcat+dubbo单机部署多台dubbo应用
前面的博客已经介绍如何使用nginx+tomcat,今天做的是如何在单台服务器上如何部署多台dubbo 应用的集群. 由于在项目中遇到了这个问题,今天就把它记录下来. 1.
- 《java小应用程序(Applet)和java应用程序(Application)分别编写的简单计算器》
Application和Java Applet的区别.Java语言是一种半编译半解释的语言.Java的用户程序分为两类:Java Application和Java Applet.这两类程序在组成结构和 ...
- 一个很奇怪的问题,程序没有改动加密参数应该也没有变化.但是两次的加密结果却不一致.md5加密问题
从图上我们看出20160803的加密结果是AAEBA9C578EA522215EAE76AFCAF250.时间是9.4分 现在我们再看这个同样的加密地址与时间结果却是另一种 31672B16..... ...
- kafka 订单应用需求
kafka的介绍就不说了,网上会找到一大堆. 为了公司做报表需要对卡券订单的销售情况做总结,所以每次下单的时候都要给卡券活动模块传递一次消息,并把订单的信息发送给活动,活动做相应的数据操作,因为数据量 ...
- TeamWork-天气美食
一. 团队情况 Hello,欢迎来到我们"Code Man"队的第一次团队作业页面,"代码侠"很明显我们是一个编程队伍,由大三在读的6位同班同学组成 ...