摘要: 重写过程中,发现这种做法能加深对递归的理解,而且reduce还体现了函数式编程是如何通过参数传递来实现命令式编程中的状态改变的.(define (imap f x . y) (if (null? y) (let recur ((x x)) (if (null? x) '() (cons (f (car x)) (recur (cdr x))))) (let recur ((x x) (y y)) (if (null? x) '() (cons... 阅读全文
posted @ 2013-10-27 23:37 LisPythoniC 阅读(651) 评论(0) 推荐(0) 编辑
摘要: 首先是sum函数.最常见的用法似乎是:>>> sum([1,2,3])6但其实这是默认首个元素是数字0.我们可以指定其他数字:>>> sum([1,2,3],100)106也可指定一个列表对象:>>> sum([[1],[2],[3]],[])[1, 2, 3]>>> sum([[1],[2],[3]],['head'])['head', 1, 2, 3]来看filter,None参数特别用法:>>> filter(None,(1,2,None,3))(1, 2, 3)&g 阅读全文
posted @ 2013-10-27 22:24 LisPythoniC 阅读(413) 评论(0) 推荐(0) 编辑
摘要: procedure:(applyprocarg1...args)Procmust be a procedure andargsmust be a list. Callsprocwith the elements of the list(append (listarg1...)args)as the actual arguments.(define (f x y z) (+ x y z));等效:(f 1 2 3)(apply f '(1 2 3))(apply f 1 '(2 3))(apply f 1 2 '(3));将发生错误:;(apply f '(1) 阅读全文
posted @ 2013-10-27 14:48 LisPythoniC 阅读(582) 评论(0) 推荐(0) 编辑
摘要: named let和递归,闭包联系十分密切.而且还是提高性能的重要手段.先来看一个make-list函数的模拟,最原始的写法大概是:(define (imake-list n member) (if (= 1 n) (cons member '()) (cons member (imake-list (- n 1) member))))这种写法的毛病在于:1.递归过程中,member变量可能需要在全局作用域中查找,比局部作用域查找要慢.2.member是一个固定的参数,然而递归过程在不断重复将它传入imake-list.这个时候你需要 named let,完美... 阅读全文
posted @ 2013-10-27 00:04 LisPythoniC 阅读(402) 评论(0) 推荐(0) 编辑