摘要: SO上看到一个比较好的问题.大概是:For example I have two dicts:Dict A:{'a':1,'b':2,'c':3}Dict B:{'b':3,'c':4,'d':5}I need a pythonic way of 'combining' two dicts such that the result is :{'a':1,'b':5,'c':7,'d':5}That is to say: 阅读全文
posted @ 2013-10-28 20:42 LisPythoniC 阅读(686) 评论(0) 推荐(0) 编辑
摘要: 该书关于深浅拷贝的论述:6.20. *Copying Python Objects and Shallow and Deep Copies"when shallow copies are made, the string is explicitly copied and a new (string) object created"这是错的.当一个容器对象被浅拷贝,字符串也是和列表一样,都只是建立引用.奇特的是,作者在这句话之前写有自相矛盾的句子:A shallow copy of an object is defined to be a newly created obje 阅读全文
posted @ 2013-10-28 15:48 LisPythoniC 阅读(209) 评论(0) 推荐(0) 编辑
摘要: > (define a '(1 2 3))> (define b (cons a '()))> b((1 2 3))> (set-car! (car b) 100)> b((100 2 3))> a(100 2 3)>从上面可以看到,(car b)和a是同一对象.这个实在是有些危险啊.如何让a和b能够相互独立呢?下面这个函数返回的list各成员和最初传入的list各成员又是相互独立的:;反序 (1 2 3) -> (3 2 1)(define (rvs x) (let recur ((x x)(res '())) (if 阅读全文
posted @ 2013-10-28 14:58 LisPythoniC 阅读(263) 评论(0) 推荐(0) 编辑
摘要: ;反序 (1 2 3) -> (3 2 1)(define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons (car x) res)))));长度 (1 2 3) -> 3(define (len x) (let recur ((x x)(y 0)) (if (null? x) y (recur (cdr x) (+ y 1)))));合并列表 (x ...) (y ...) -> (x ... y ...)(define... 阅读全文
posted @ 2013-10-28 14:16 LisPythoniC 阅读(223) 评论(0) 推荐(0) 编辑
摘要: > (define (f x) x)> (define (g x) (let rec((x x)) x))> (define a '(1 2 3))> (f a)(1 2 3)> (eq? a (f a))#t> (eq? a (g a))#t> (define b (g a))> (set-car! b 10)> a(10 2 3)> 可见,g函数的定义中,named let并未深拷贝x的值,它只是建立传入参数的引用而已.那这也说明,如果一个函数所有参数在递归过程中都会发生改变,那么一般没必要用named let.又如:;切 阅读全文
posted @ 2013-10-28 13:51 LisPythoniC 阅读(377) 评论(0) 推荐(0) 编辑
摘要: 用到的元素有9个:define,if,null?,cons car,cdr,lambda,let,named let,其实let 和 named let可以去掉.但那样会带来性能和可读性下降的问题.排序类型选的是经典的快速排序.;筛选函数(define (filter f x) (let recur ((x x)) (if (null? x) '() (if (f (car x)) (cons (car x) (recur (cdr x))) (recur (cdr x))))));三元合并函数,形如'(1 2) ... 阅读全文
posted @ 2013-10-28 01:50 LisPythoniC 阅读(612) 评论(0) 推荐(0) 编辑