> |
(define one-through-four (list 1 2 3 4)) |
> |
(car one-through-four) |
> |
(cdr one-through-four) |
> |
(car (cdr one-through-four)) |
> |
(cons 10 one-through-four) |
> |
(cons 5 one-through-four) |
> |
(define (list-ref items n) (if (= n 0) (car items) (list-ref (cdr items) (- n 1)))) |
> |
(define squares (list 1 4 9 16 25)) |
> |
(list-ref squares 3) |
> |
(define (length items) (if (null? items) 0 (+ 1 (length (cdr items))))) |
> |
(define odds (list 1 3 5 7)) |
> |
(length odds) |
> |
(append squares odds) |
> |
(append odds squares) |
> |
(define (scale-list items factor) (if (null? items) () (cons (* (car items) factor) (scale-list (cdr items) factor)))) |
> |
(scale-list (list 1 2 3 4 5) 10) |
> |
(map abs (list -10 2.5 -11.6 17)) |
> |
(map (lambda (x) (* x x)) (list 1 2 3 4)) |
> |
> |
(define (count-leaves x) (cond ((null? x) 0) ((not (pair? x)) 1) (else (+ (count-leaves (car x)) (count-leaves (cdr x)))))) |
> |
(define x (cons (list 1 2) (list 3 4))) |
> |
(length x) |
> |
(count-leaves x) |
> |
(list x x) |
> |
(length (list x x)) |
> |
(count-leaves (list x x)) |
> |
(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 (list 1 (list 2 (list 3 4) 5) (list 6 7)) 10) |
> |
> |
(define (square x) (* x x)) |
> |
(map square (list 1 2 3 4 5)) |
> |
(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)) |
> |
(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)) |
> |
(accumulate * 1 (list 1 2 3 4 5)) |
> |
(accumulate cons () (list 1 2 3 4 5)) |
> |
(define (enumerate-interval low high) (if (> low high) () (cons low (enumerate-interval (+ low 1) high)))) |
> |
(enumerate-interval 2 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 (list 1 (list 2 (list 3 4)) 5)) |
> |
(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) |
> |
(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)) |
> |
> |
(define (square x) (* x x)) |
> |
(define (smallest-divisor n) (find-divisor n 2)) |
> |
(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))))) |
> |
(define (divides? a b) (= (remainder b a) 0)) |
> |
(define (prime? n) (= n (smallest-divisor n))) |
> |
(define (flatmap proc seq) (accumulate append () (map proc seq))) |
> |
(define (prime-sum? pair) (prime? (+ (car pair) (cadr pair)))) |
> |
(define (make-pair-sum pair) (list (car pair) (cadr pair) (+ (car pair) (cadr pair)))) |
> |
(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 4) |
> |
(define (remove item sequence) (filter (lambda (x) (not (= x item))) sequence)) |
> |
(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 (list 1 2 3)) |
> |