Sunday, May 16, 2010

Busy week

I really wanted to get a lot more done this week, but final result is that still not done with chapter 2.2

Getting started during a holiday at least helped get some speed... Now I need another to maintain it.

Here's 2.41 just for the hell of it


(define (triples n)
(flatmap (lambda (i)
(map (lambda (j) (cons i j))
(unique-pairs (- i 1))))
(enumerate-interval 1 n)))
(define (s-triples target max)
(filter (lambda (x) (= target (accumulate + 0 x))) (triples max)))


Also some definitions that might be needed:


(define (unique-pairs n)
(flatmap (lambda (i)
(map (lambda (j) (list i j))
(enumerate-interval 1 (- i 1))))
(enumerate-interval 1 n)))

(define (enumerate-interval start n)
(define (enumerate x)
(if (< n x)
null
(cons x (enumerate (+ 1 x)))))
(enumerate start))

(define (filter predicate sequence)
(cond ((null? sequence) null)
((predicate (car sequence))
(cons (car sequence)
(filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence)))))

No comments: