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. 运行impala tpch

    1.安装git和下载tpc-h-impala脚步 [root@ip-172-31-34-31 ~]# yum install git [root@ip-172-31-34-31 ~]# git clo ...

  2. less和sass的介绍和差异

    ● 混入(Mixins)——class中的class: ● 参数混入——可以传递参数的class,就像函数一样: ● 嵌套规则——Class中嵌套class,从而减少重复的代码: ● 运算——CSS中 ...

  3. 不懂指针就不要说自己学过C语言!

    不懂指针就不要说自己学过C语言! 1.掌握了指针,就掌握了C语言的精髓!计算机中绝大部分数据都放到内存中的,不同的数据放到不同的内存区域中. 内存角度没有数据类型,只有二进制:数据以字节(8位二进制) ...

  4. win7下KiWi Syslog服务器的安装与配置

    今天就来聊聊日志服务器KiWi Syslog的安装与配置. 首先,所需文件有以下2个: 1.Kiwi_Syslog_Server_9.5.0.Eval.setup.exe[此版本只有14天寿命][Ki ...

  5. JavaScript数据类型

    当说到JavaScript的数据类型时,好多做了很多年web的朋友甚至还不太清楚.这应该算是最基础的js知识了.我觉得有必要在这记录一下,方便自己和大家查看. 最基本的,js有五种数据类型:undef ...

  6. ubuntu下code::blocks+opengl的使用与配置

    操作系统:Ubuntu 15.04 gcc version 4.9.2 opengl安装 sudo apt-get install build-essential libgl1-mesa-dev li ...

  7. 在web.config配置中添加xml内容

    在web.config 中添加需要的内容时, 就是在<configuration>节点内添加一个新的<configSections>元素, 在configSections元素中 ...

  8. JCS缓存使用类

    项目是一个门户网站,一些新闻等会做缓存. 导入jar包: 项目跟目录下配置文件:cache.ccf #内存缓存 jcs.default= jcs.default.cacheattributes=org ...

  9. JS第一天基础总结

    今天开始学习javascript 主要讲理论上的东西,例如,什么是脚本语言,什么是变量,什么是DOM,什么是BOM等. 脚本语言,实质上可以理解为我们编写的剧本,浏览器按照我们编写的剧本一步一步完成剧 ...

  10. uboot(二): Uboot-arm-start.s分析

    声明:该贴是通过参考其他人的帖子整理出来,从中我加深了对uboot的理解,我知道对其他人一定也是有很大的帮助,不敢私藏,如果里面的注释有什么错误请给我回复,我再加以修改.有些部分可能还没解释清楚,如果 ...