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. 替换url中某个参数的值或是添加某个参数的方法(js 分页上下页可以使用)

    function changeUrl(base, find, value) { var offset = base.indexOf(find); var index; var rr = ''; if( ...

  2. 取得交换机的MAC地址

    一.首先PING交换机的IP地址 二.然后执行 ARP -a 命令 如下图:

  3. powershell中使用超大内存对象

    powershell中使用超大内存对象 简单介绍了powershell中超大内存对象的用途,开启powershell超大内存对象的办法. powershell 传教士 原创文章 2016-12-31 ...

  4. android studio 换护眼的颜色步骤

    设置--->Editor-->General-->Default Text-->Background护眼色是#D2E3C7

  5. x.1

    最近公司人事变动略频 昨日老板召集众骨干动员,谈心,表示有信心,没资金压力. 今日各种谈心,唉…… 人事姐姐约逻辑组长聊,美术主管就找上了我,一通倾述.内容实事求是,但是行业内各公司都这样,唉,还想着 ...

  6. 探索javascript----拖拽

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 解决ie6 fixed 定位以及抖动问题

    像你所遇到的问题一样, IE6浏览器有太多的bug让制作网页的人头疼.这篇文章介绍的是介绍的是如何解决IE6不支持position:fixed;属性的办法.如果我们需要做某个元素始终位于浏览器的底部, ...

  8. MySQL数据库3 - MySQL常用数据类型

    一. MySql常用数据类型 数据类型:整数(tinyint smailint int bigint) 定点数 decimal(p,s) ------ 小数点位置固定的       ---> 数 ...

  9. 内存对齐 和 sizeof小结

    数据对齐(内存对齐)指该数据所在的地址必须是该数据长度的整数倍.X86CPU能直接访问对齐的数据,当它试图访问未对齐的数据时,会在内部进行一系列的调整,降低运行速度.数据对齐一般出现在结构体和类中,在 ...

  10. i++与++i

    #include <stdio.h> int main() { int a,b,c,d; a = 10; b = a++;//相当于两个句子:b = a,a += 1;先使用a的值再加1 ...