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的更多相关文章

  1. scheme和common lisp 区别

    Scheme and Common Lisp use different names for some of the basic system functions. Many Lisp program ...

  2. 10994 - Simple Addition(规律)

    Problem E Simple Addition Input: Standard Input Output: Standard Output Let’s define a simple recurs ...

  3. Bloomberg面经准备: Josephus problem

    Given a circular single linked list.Write a program that deletes every kth node until only one node ...

  4. java源码剖析: 对象内存布局、JVM锁以及优化

    一.目录 1.启蒙知识预热:CAS原理+JVM对象头内存存储结构 2.JVM中锁优化:锁粗化.锁消除.偏向锁.轻量级锁.自旋锁. 3.总结:偏向锁.轻量级锁,重量级锁的优缺点. 二.启蒙知识预热 开启 ...

  5. Why Doesn't Python Have Switch/Case?

    Why Doesn't Python Have Switch/Case? Tuesday, June 09, 2015 (permalink) Unlike every other programmi ...

  6. Dynamic Programming | Set 1 (Overlapping Subproblems Property)

    动态规划是这样一种算法范式:将复杂问题划分为子问题来求解,并且将子问题的结果保存下来以避免重复计算.如果一个问题拥有以下两种性质,则建议使用动态规划来求解. 1 重叠子问题(Overlapping S ...

  7. [Optimization] Advanced Dynamic programming

    这里主要是较为详细地理解动态规划的思想,思考一些高质量的案例,同时也响应如下这么一句口号: “迭代(regression)是人,递归(recursion)是神!” Video series for D ...

  8. jdk源码剖析二: 对象内存布局、synchronized终极原理

    很多人一提到锁,自然第一个想到了synchronized,但一直不懂源码实现,现特地追踪到C++层来剥开synchronized的面纱. 网上的很多描述大都不全,让人看了不够爽,看完本章,你将彻底了解 ...

  9. ElasticSearch 2 (5) - Document APIs

    ElasticSearch 2.1.1 (5) - Document APIs This section describes the following CRUD APIs: Single docu ...

随机推荐

  1. WebForm使用AngularJS实现下拉框多级联动

    数据准备 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,                                             CateId = ,        ...

  2. java web学习之初识jsp

    用java语言(+html语言)开发动态资源的技术: jsp的运行过程,1:tomcat服务器将jsp代码翻译成java代码,并且编译成class文件 2:tomcat服务器构造类对象 3:tomca ...

  3. 事件(event),正则

    1.事件(event):事件是可以被 JavaScript 侦测到的行为.网页中的每个元素都可以产生某些可以触发 JavaScript 函数的事件.2.事件源: 触发事件的元素 事件: 被 JavaS ...

  4. android-oldman之TitleBar

    随着大众口味的不断提高,对app要的开发的要求也不断提高,开发人员们要在app上展示的东西安也越来越多,android早期的一些控件慢慢的不不能满足开发的要求了,比如TitleBar的应用的就没有原来 ...

  5. my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏

    If you understand the comments below, never will you make mistakes with binary search! thanks to A s ...

  6. 利用Volley封装好的图片缓存处理加载图片

    Volley 工具箱中提供了一种通过 DiskBasedCache 类实现的标准缓存.这个类能够缓存文件到磁盘的指定目录.但是为了使用 ImageLoader,我们应该提供一个自定义的内存 LRC b ...

  7. Dapper学习笔记(3)-增、删、改、查

    一.建表 在数据库中建立如下三张表: CREATE TABLE [dbo].[T_User] ( , ) PRIMARY KEY NOT NULL, ) NOT NULL, ) NULL, ) NUL ...

  8. mongo 导入json数据

    删除库以及导入库

  9. Java集合运用技巧

    需要唯一吗? 需要:Set 需要制定顺序吗? 需要:TreeSet 不需要:HashSet 但是想要一个和存储一致的顺序(有序):LinkedHashSet 不需要:List 需要频繁增删吗? 需要: ...

  10. sqoop笔记

    adoop学习笔记—18.Sqoop框架学习   一.Sqoop基础:连接关系型数据库与Hadoop的桥梁 1.1 Sqoop的基本概念 Hadoop正成为企业用于大数据分析的最热门选择,但想将你的数 ...