Representing Sequences

> 
(define one-through-four (list 1 2 3 4))

(1 2 3 4)

> 
(car one-through-four)

1

> 
(cdr one-through-four)

(2 3 4)

> 
(car (cdr one-through-four))

2

> 
(cons 10 one-through-four)

(10 1 2 3 4)

> 
(cons 5 one-through-four)

(5 1 2 3 4)

> 
(define (list-ref items n)
  (if (= n 0)
      (car items)
      (list-ref (cdr items) (- n 1))))

list-ref

> 
(define squares (list 1 4 9 16 25))

(1 4 9 16 25)

> 
(list-ref squares 3)

16

> 
(define (length items)
  (if (null? items)
      0
      (+ 1 (length (cdr items)))))

length

> 
(define odds (list 1 3 5 7))

(1 3 5 7)

> 
(length odds)

4

> 
(append squares odds)

(1 4 9 16 25 1 3 5 7)

> 
(append odds squares)

(1 3 5 7 1 4 9 16 25)

> 
(define (scale-list items factor)
  (if (null? items)
      ()
      (cons (* (car items) factor)
            (scale-list (cdr items) factor))))

scale-list

> 
(scale-list (list 1 2 3 4 5) 10)

(10 20 30 40 50)

> 
(map abs (list -10 2.5 -11.6 17))

(10 2.5 11.6 17)

> 
(map (lambda (x) (* x x))
     (list 1 2 3 4))

(1 4 9 16)

> 

Hierarchical Structures

> 
(define (count-leaves x)
  (cond ((null? x) 0)  
        ((not (pair? x)) 1)
        (else (+ (count-leaves (car x))
                 (count-leaves (cdr x))))))

count-leaves

> 
(define x (cons (list 1 2) (list 3 4)))

((1 2) 3 4)

> 
(length x)

3

> 
(count-leaves x)

4

> 
(list x x)

(((1 2) 3 4) ((1 2) 3 4))

> 
(length (list x x))

2

> 
(count-leaves (list x x))

8

> 
(define (scale-tree tree factor)
  (cond ((null? tree) ())
        ((not (pair? tree)) (* tree factor))
        (else (cons (scale-tree (car tree) factor)
                    (scale-tree (cdr tree) factor)))))

scale-tree

> 
(scale-tree (list 1 (list 2 (list 3 4) 5) (list 6 7))
            10)

(10 (20 (30 40) 50) (60 70))

> 

Sequences as Conventional Interfaces

Sequence Operations

> 
(define (square x) (* x x))

square

> 
(map square (list 1 2 3 4 5))

(1 4 9 16 25)

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

filter

> 
(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

> 
(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

> 
(enumerate-interval 2 7)

(2 3 4 5 6 7)

> 
(define (enumerate-tree tree)
  (cond ((null? tree) ())
        ((not (pair? tree)) (list tree))
        (else (append (enumerate-tree (car tree))
                      (enumerate-tree (cdr tree))))))

enumerate-tree

> 
(enumerate-tree (list 1 (list 2 (list 3 4)) 5))

(1 2 3 4 5)

> 
(define (fib n)
  (cond ((= n 0) 0)
        ((= n 1) 1)
        (else (+ (fib (- n 1))
                 (fib (- n 2))))))

fib

> 
(define (list-fib-squares n)
  (accumulate cons
              ()
              (map square
                   (map fib
                        (enumerate-interval 0 n)))))

list-fib-squares

> 
(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

> 
(product-of-squares-of-odd-elements (list 1 2 3 4 5))

225

> 

Nested Mappings

> 
(define (square x) (* x x))

square

> 
(define (smallest-divisor n)
  (find-divisor n 2))

smallest-divisor

> 
(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (+ test-divisor 1)))))

find-divisor

> 
(define (divides? a b)
  (= (remainder b a) 0))

divides?

> 
(define (prime? n)
  (= n (smallest-divisor n)))

prime?

> 
(define (flatmap proc seq)
  (accumulate append () (map proc seq)))

flatmap

> 
(define (prime-sum? pair)
  (prime? (+ (car pair) (cadr pair))))

prime-sum?

> 
(define (make-pair-sum pair)
  (list (car pair) (cadr pair) (+ (car pair) (cadr pair))))

make-pair-sum

> 
(define (prime-sum-pairs n)
  (map make-pair-sum
       (filter prime-sum?
               (flatmap
                (lambda (i)
                  (map (lambda (j) (list i j))
                       (enumerate-interval 1 (- i 1))))
                (enumerate-interval 1 n)))))

prime-sum-pairs

> 
(prime-sum-pairs 4)

((2 1 3) (3 2 5) (4 1 5) (4 3 7))

> 
(define (remove item sequence)
  (filter (lambda (x) (not (= x item)))
          sequence))

remove

> 
(define (permutations s)
  (if (null? s)                   ; empty set?
      (list ())                   ; sequence containing empty set
      (flatmap (lambda (x)
                 (map (lambda (p) (cons x p))
                      (permutations (remove x s))))
               s)))

permutations

> 
(permutations (list 1 2 3))

((1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1))

>