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. .netcore跨平台 之 windows上编译,ubuntu上运行

    1 下载并安装netcore sdk    下载地址 https://github.com/dotnet/cli 选取合适的版本下载安装即可 打开 CMD ,输入dotnet,出现以下信息说明已安装好 ...

  2. JS技术大全

    事件源对象:event.srcElement.tagName  event.srcElement.type 捕获/释放:event.srcElement.setCapture();  event.sr ...

  3. GDB中汇编调试

    GDB中汇编调试 1.输入代码 2.使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,时遇到问题使用-m32指令报错,参考卢肖明同学博客知道这是 ...

  4. android editText 监听事件

    在软键盘中注意 在监听的 edittext中 使用android:imeOptions属性的时候,一定要对EditText设置 android:inputType 或者 设置 android:sing ...

  5. Linq中关键字的作用及用法

    Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...

  6. It will affect staff as well.

    Premier Foods has reduced its number of suppliers dramatically in the last 12 months. In 2013 it mad ...

  7. C#对Dictionary的按Value排序

    使用List对其进行排序 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp ...

  8. patchca验证码的使用

    /** * 生成验证码 */ private static RandomFontFactory ff = null; // 自定义验证码图片背景 private static MyCustomBack ...

  9. ObjC宏定义小细节

    Macros A definition that takes arguments, particularly more than one, is often known as a macro: #de ...

  10. BackgroundWorker组件的作用

    当构建一个图形化的Windows Form桌面应用程序并且需要执行在应用程序主UI线程之外的线程中长时间的任务时,BackgroundWorker类就很有用了. 要使用BackgroundWorker ...