SICPの2.2.3 Sequences as Conventional Interfaces(慣習的な作用する配列) の Sequence Operations(配列操作) あたりを Python でやってみた
scheme
;2.2.3 Sequences as Conventional Interfaces ;Sequence Operations (define (filter predicate sequence) (cond ((null? sequence) ()) ((predicate (car sequence)) (cons (car sequence) (filter predicate (cdr sequence)))) (else (filter predicate (cdr sequence))))) (filter odd? (list 1 2 3 4 5));(1 3 5) (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) (accumulate + 0 (list 1 2 3 4 5));15 (accumulate * 1 (list 1 2 3 4 5));120 (accumulate cons () (list 1 2 3 4 5));(1 2 3 4 5) (define (enumerate-interval low high) (if (> low high) () (cons low (enumerate-interval (+ low 1) high)))) (enumerate-interval 2 7);(2 3 4 5 6 7) To enumerate the leaves of a tree, we can use14 (define (enumerate-tree tree) (cond ((null? tree) ()) ((not (pair? tree)) (list tree)) (else (append (enumerate-tree (car tree)) (enumerate-tree (cdr tree)))))) (enumerate-tree (list 1 (list 2 (list 3 4)) 5));(1 2 3 4 5) ;; 1.1.4 Compound Procedures (define (square x)(* x x)) ;; 1.2.2 Tree Recursion (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) (define (list-fib-squares n) (accumulate cons () (map square (map fib (enumerate-interval 0 n))))) (list-fib-squares 10);(0 1 1 4 9 25 64 169 441 1156 3025) (define (product-of-squares-of-odd-elements sequence) (accumulate * 1 (map square (filter odd? sequence)))) (product-of-squares-of-odd-elements (list 1 2 3 4 5));225
python
def filter(prodicate, sequence): if not sequence: return [] if prodicate(sequence[0]): return [sequence[0]] + filter(prodicate, sequence[1:]) return filter(prodicate, sequence[1:]) #odd? == lambda x:x%2 print filter(lambda x:x%2, [1,2,3,4,5]) #[1, 3, 5] def accumulate(op, initial, sequence): if not sequence: return initial return op(sequence[0], accumulate(op, initial, sequence[1:])) print accumulate(lambda x,y:x + y, 0, [1,2,3,4,5])#15 print accumulate(lambda x,y:x * y, 1, [1,2,3,4,5])#120 print accumulate(lambda x,y:[x] + y, [], [1,2,3,4,5])#[1, 2, 3, 4, 5] def enumerate_interval(low, high): if low > high: return [] return [low] + enumerate_interval(low + 1, high) print enumerate_interval(2, 7)#[2, 3, 4, 5, 6, 7] # def enumerate_tree(tree): if not tree: return [] if not isinstance(tree, list): return [tree] return enumerate_tree(tree[0]) + enumerate_tree(tree[1:]) print enumerate_tree([1,[2,[3,4]],5])#[1, 2, 3, 4, 5] #1.1.4 Compound Procedures def square(x): return x * x #1.2.2 Tree Recursion def fib(n): if n == 0: return 0 if n == 1: return 1 return fib(n - 1) + fib(n - 2) def list_fib_squares(n): return accumulate(lambda x,y:[x] + y, [], map(square, map(fib, enumerate_interval(0,n)))) print list_fib_squares(10)#[0, 1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025] def product_of_squares_of_odd_elements(sequence): return accumulate(lambda x,y:x * y, 1, map(square, filter(lambda x:x % 2, sequence))) print product_of_squares_of_odd_elements([1,2,3,4,5])#225