collect - ,result)) ,(if (null? restriction)...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
(define (make-collect-body result bindings restriction) (define (collect-let-bindings bindings n) (if (null? bindings) '() (cons `(,(caar bindings) (car ,((repeated (lambda (x) `(cdr ,x)) n) 'tuple))) (collect-let-bindings (cdr bindings) (+ n 1))))) (define (collect-let-body rest-of-bindings) (if (null? (cdr rest-of-bindings)) `(map (lambda (,(caar rest-of-bindings)) (list ,@(map car bindings))) ,(cadar rest-of-bindings)) `(flatmap (lambda (,(caar rest-of-bindings)) ,(collect-let-body (cdr rest-of-bindings))) ,(cadar rest-of-bindings)))) `(map (lambda (tuple) (let ,(collect-let-bindings bindings 0)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ,result)) ,(if (null? restriction) (collect-let-body bindings) `(filter (lambda (tuple) (let ,(collect-let-bindings bindings 0) ,(car restriction))) ,(collect-let-body bindings))))) (define-macro (collect result bindings . restriction) (make-collect-body result bindings restriction)) (define (prime-sum-pairs n) (collect (list i j (+ i j)) ((i (enumerate-interval 1 n)) (j (enumerate-interval 1 (-1+ i)))) (prime? (+ i j)))) (define (prime? n) (define (iter factor) (cond ((= factor n) #t) ((= (remainder n factor) 0) #f) (else (iter (1+ factor))))) (iter 2))...
View Full Document

This note was uploaded on 11/30/2010 for the course EECS 21281 taught by Professor Harvey during the Spring '10 term at University of California, Berkeley.

Ask a homework question - tutors are online