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. GitLab在Centos下的安装步骤

    第一步:(安装工具包) sudo yum install curl openssh-server postfix cronie sudo service postfix start sudo chkc ...

  2. mac brew mysql 启动之后报错

    打开电脑 链接mysql 发现报错,连不上,应该是没自启动, 之前一直用windows电脑,就用mysql start 准备启动下,发现报错, p.p1 { margin: 0.0px 0.0px 0 ...

  3. shell 随机从数组中抽取三个随机数(#可持续不停抽取)

    #!/bin/bash #b= ]] #do #sleep 1 student=( DPL YPD LT ZZM HY CQW LSJ ybr) a=$[RANDOM%+] c=$[RANDOM%+] ...

  4. git中.gitignore配置项不起作用-解决办法

    在某个git项目中,.gitignore忽略了*.iml,但是git status命令依然列了出来,最后发现是由于git的缓存造成的. git rm -r --cached . git add . g ...

  5. 《java异常的一些总结》

    关于Java中异常的一些总结: 3 有些时候,程序在try块里打开了一些物理资源(例如数据库连接,网络连接. 4 和磁盘文件等),这些物理资源都必须显示回收. 5 6 注意:Java的垃圾回收机制不会 ...

  6. Java—工厂模式

    工厂模式: public interface Cookie { public abstract void run (); } public abstract class CookieFactory { ...

  7. nodejs渲染模板

    为什么要用nodejs来渲染? 之前前端的任务就是用HTML+CSS 来高保真的还原UI所设计原图,偶尔会使用少量的jq来对页面添加一些特效,页面还要交付给后端开发人员进行数据填充(php jsp)等 ...

  8. NSOperation操作依赖和监听

    1.操作依赖 NSOperation之间可以设置依赖来保证执行顺序 比如一定要让操作A执行完后,才能执行操作B,可以这么写 [operationB addDependency:operationA]; ...

  9. jsp内置对象浅谈

    jsp内置对象浅谈 | 浏览:1184 | 更新:2013-12-11 16:01 JSP内置对象:我们在使用JSP进行页面编程时可以直接使用而不需自己创建的一些Web容器已为用户创建好的JSP内置对 ...

  10. 转发 java数据结构之hashMap详解

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...