A procedure body can contain calls to other procedures, not least itself:

(define factorial
(lambda (n)
(if (= n 0) 1
(* n (factorial (- n 1))))))

This recursive procedure calculates the factorial of a number. If the number is 0, the answer is 1. For any other number n, the procedure uses itself to calculate the factorial of n ‑ 1, multiplies that subresult by n, and returns the product.

Mutually recursive procedures are also possible. The following predicates for evenness and oddness use each other:

相互递归:

(define is-even?
(lambda (n)
(if (= n 0) #t
(is-odd? (- n 1))))) (define is-odd?
(lambda (n)
(if (= n 0) #f
(is-even? (- n 1)))))

These definitions are offered here only as simple illustrations of mutual recursion. Scheme already provides the primitive predicates even? and odd?.

6.1  letrec

If we wanted the above procedures as local variables, we could try to use alet form:

(let ((local-even? (lambda (n)
(if (= n 0) #t
(local-odd? (- n 1)))))
(local-odd? (lambda (n)
(if (= n 0) #f
(local-even? (- n 1))))))
(list (local-even? 23) (local-odd? 23)))

This won’t quite work, because the occurrences of local‑even? and local‑odd?in the initializations don’t refer to the lexical variables themselves. Changing the let to a let* won’t work either, for while the local‑even?inside local‑odd?’s body refers to the correct procedure value, thelocal‑odd? in local‑even?’s body still points elsewhere.

To solve problems like this, Scheme provides the form letrec:

(letrec ((local-even? (lambda (n)
(if (= n 0) #t
(local-odd? (- n 1)))))
(local-odd? (lambda (n)
(if (= n 0) #f
(local-even? (- n 1))))))
(list (local-even? 23) (local-odd? 23)))

The lexical variables introduced by a letrec are visible not only in theletrec-body but also within all the initializations. letrec is thus tailor-made for defining recursive and mutually recursive local procedures.

letrec专门用来定义为定义递归和相互递归的局部过程使用。

6.2  Named let

A recursive procedure defined using letrec can describe loops. Let’s say we want to display a countdown from 10:

(letrec ((countdown (lambda (i)
(if (= i 0) 'liftoff
(begin
(display i)
(newline)
(countdown (- i 1)))))))
(countdown 10))

This outputs on the console the numbers 10 down to 1, and returns the result liftoff.

Scheme allows a variant of let called named let to write this kind of loop more compactly:

scheme有一个named let可以让let有一个名字。

(let countdown ((i 10))
(if (= i 0) 'liftoff
(begin
(display i)
(newline)
(countdown (- i 1)))))

Note the presence of a variable identifying the loop immediately after thelet. This program is equivalent to the one written with letrec. You may consider the named let to be a macro (chap 8) expanding to the letrec form.

现在变量名countdown立即表示了整个loop,等价于上面用letrec的。

6.3  Iteration

countdown defined above is really a recursive procedure. Scheme can define loops only through recursion. There are no special looping or iteration constructs.

schemem只能通过递归来定义循环,没有loop或其他的循环购置。

Nevertheless, the loop as defined above is a genuine loop, in exactly the same way that other languages bill their loops. Ie, Scheme takes special care to ensure that recursion of the type used above will not generate the procedure call/return overhead.

Scheme does this by a process called tail-call elimination. If you look closely at the countdown procedure, you will note that when the recursive call occurs in countdown’s body, it is the tail call, or the very last thing done — each invocation of countdown either does not call itself, or when it does, it does so as its very last act. To a Scheme implementation, this makes the recursion indistinguishable from iteration. So go ahead, use recursion to write loops. It’s safe.

上面定义的 countdown 是递归过程,scheme 没有 C 语言中的 for、while 循环、迭代语句,而是用递归过程来实现循环、迭代的。
 
在其他语言中,用递归来实现循环,从而消耗很大的内存,scheme 则对上面类似的递归调用特殊处理,使其内存消耗为一个常量,这个主要是因为 scheme 支持尾调用。如果你细心观察,在 countdown 过程体中,每次调用自身都是尾调用或者是在过程体最后的一个表达式,这就是尾调用,而 scheme 中的尾调用不会消耗额外的内存空间,所以,在 scheme 中,请放心用递归来实现循环。
 
下面是一个尾递归<也是尾调用>的例子:

Here’s another example of a useful tail-recursive procedure:

(define list-position
(lambda (o l)
(let loop ((i ) (l l))
(if (null? l) #f
(if (eqv? (car l) o) i
(loop (+ i ) (cdr l)))))))

list‑position finds the index of the first occurrence of the object o in the list l. If the object is not found in the list, the procedure returns #f. list-position找出list表l中对象o第一次出现的位置。

Here’s yet another tail-recursive procedure, one that reverses its argument list “in place”, ie, by mutating the contents of the existing list, and without allocating a new one:

通过改变(mutate)存在的list,不用分配内存:

(define reverse!
(lambda (s)
(let loop ((s s) (r '()))
(if (null? s) r
(let ((d (cdr s)))
(set-cdr! s r)
(loop d s))))))

(reverse! is a useful enough procedure that it is provided primitively in many Scheme dialects, eg, MzScheme and Guile.)

For some numerical examples of recursion (including iteration), see Appendix C.

上面的返回给定列表的倒序列表,但是所给定列表会发生改变,若不想改变原列表,请看下面的定义:
 
1
2
3
4
5
6
7
8
9
10
11
12
scheme@(guile-user)> (define list-reverse
...                     (lambda (ls)
...                        (let loop ((ls ls) (xs '()))
...                           (if (null? ls)
...                              xs
...                              (loop (cdr ls)
...                                    (cons (car ls) xs))))))
scheme@(guile-user)> (define ls '(1 2 3 4))
scheme@(guile-user)> (list-reverse ls)
(4 3 2 1)
scheme@(guile-user)> ls
(1 2 3 4)

6.4  Mapping a procedure across a list

A special kind of iteration involves repeating the same action for each element of a list. Scheme offers two procedures for this situation: map andfor‑each.

The map procedure applies a given procedure to every element of a given list, and returns the list of the results. Eg,

(map add2 '(1 2 3))
=> (3 4 5)

The for‑each procedure also applies a procedure to each element in a list, but returns void. The procedure application is done purely for any side-effects it may cause. Eg,

 for-each 函数主要是为了得到函数的副作用:

(for-each display
(list 1 2 3))
输出:1 2 3
如果用map会输出:
123(#<void> #<void> #<void>) (Racket下)

has the side-effect of displaying the strings (in the order they appear) on the console.

The procedures applied by map and for‑each need not be one-argument procedures. For example, given an n-argument procedure, map takes n lists and applies the procedure to every set of n of arguments selected from across the lists. Eg,

map和for-each的存储过程参数不必是一个参数的存储过程,可以是n个。

(map cons '(1 2 3) '(10 20 30))
=> ((1 . 10) (2 . 20) (3 . 30)) (map + '(1 2 3) '(10 20 30))
=> (11 22 33)
参考:http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-8.html
http://lispor.is-programmer.com/posts/23255.html

Teach Yourself Scheme in Fixnum Days 6 recursion递归的更多相关文章

  1. Teach Yourself Scheme in Fixnum Days 13 Jump跳转

    Jumps One of the signal features of Scheme is its support for jumps or nonlocal control. Specificall ...

  2. recursion 递归以及递归的缺点

    递归定义的算法有两部分: 递归基:直接定义最简单情况下的函数值: 递归步:通过较为简单情况下的函数值定义一般情况下的函数值. 应用条件与准则: (1)问题具有某种可借用的类同自身的子问题描述的性质: ...

  3. string formating字符串格式化,function函数,group组,recursion递归,练习

    # -*- coding: UTF-8 -*- msg = 'i am {} my hobby is {}'.format('lhf',18) print(msg) msg1 = 'i am %s m ...

  4. Recursion递归

    /*java.lang 核心包 如 String Math Integer System Thread等 拿来直接用 * java.awt 窗口工具 GUI * java.net 网络包 * java ...

  5. Github上的1000多本免费电子书重磅来袭!

    Github上的1000多本免费电子书重磅来袭!   以前 StackOverFlow 也给出了一个免费电子书列表,现在在Github上可以看到时刻保持更新的列表了. 瞥一眼下面的书籍分类目录,你就能 ...

  6. Github 的一个免费编程书籍列表

    Index Ada Agda Alef Android APL Arduino ASP.NET MVC Assembly Language Non-X86 AutoHotkey Autotools A ...

  7. Lisp语言学习的书

    Scheme <How to Design Programs : An Introduction to Programming and Computing>(<程序设计方法>) ...

  8. racket学习-call/cc (let/cc)

    Drracket continuation 文中使用let/cc代替call/cc Racket文档中,let/cc说明为: (let/cc k body ...+) Equivalent to (c ...

  9. 开始学习Scheme

    开始学习Scheme   函数式编程(Functional Programming)是在MIT研究人工智能(Artificial Intelligence)时发明的,其编程语言为Lisp.确切地说,L ...

随机推荐

  1. httpd与tomcat基于mod_jk整合

    搞定在前面述, httpd与tomcat整合方式 当前已知的有 ajp_proxy,mod_jk.so jk connecteor连接器下载地址 http://archive.apache.org/d ...

  2. 上海游侠电动汽车团队招募。iOS,Android,产品经理以及 SEVER 端工程师 - V2EX

    上海游侠电动汽车团队招募.iOS,Android,产品经理以及 SEVER 端工程师 - V2EX 上海游侠电动汽车团队招募.iOS,Android,产品经理以及 SEVER 端工程师

  3. 高性能Java解析器实现过程详解

    如果你没有指定数据或语言标准的或开源的Java解析器, 可能经常要用Java实现你自己的数据或语言解析器.或者,可能有很多解析器可选,但是要么太慢,要么太耗内存,或者没有你需要的特定功能.或者开源解析 ...

  4. 黑马程序员_<<IO流基本操作(Writer,Reader)>>

    --------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1.概述 硬盘之间的文件的传输,硬盘中文件的 ...

  5. Python模拟登录实战(一)

    今天,学习了模拟登录新浪微博.模拟登录主要有两种方式,一.利用Cookie:二.模仿浏览器的请求,发送表单. 法一: Cookie:指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密 ...

  6. js中的数据类型及其转换

    Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...

  7. wsdl文件结构分析

    WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些服务 ...

  8. mysql简单练习

    数据库入门 2.1 引入 数据保存到内存: 优点: 1)读写非常快 缺点: 1)程序关闭导致数据丢失 数据保存到文件: 优点: 1)数据可以永久保存 缺点: 1)频繁地IO操作,效率不高! 2)数据管 ...

  9. zeptoJS:如何像jQuery一样,让滚动变得优雅?

    利用jQuery的animate() 方法,我们很容易实现滚动条的平滑滚动效果: $(function() { $('#top').click( function (e) { $('html, bod ...

  10. Android Clipboard(复制/剪贴板)

    Android提供的剪贴板框架,复制和粘贴不同类型的数据.数据可以是文本,图像,二进制流数据或其它复杂的数据类型. Android提供ClipboardManager.ClipData.Item和Cl ...