You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

#### 79 lines 2.2 KiB Raw Blame History

 `(newline)` `(display "Exercise 2.40")(newline)` `; -- Defs --` `(define nil ())` `(define (accumulate op initial sequence)` ` (if (null? sequence)` ` initial` ` (op (car sequence)` ` (accumulate op initial (cdr sequence)))))` `; -- Start --` `(define (flatmap proc seq)` ` (accumulate append nil (map proc seq)))` `; --------` ``` ``` `(define (permutations s)` ` (if (null? s) ; empty set?` ` (list nil) ; sequence containing empty set` ` (flatmap (lambda (x)` ` (map (lambda (p) (cons x p))` ` (permutations (remove x s))))` ` s)))` `(define (remove item sequence) ` ` (filter (lambda (x) (not (= item x))) ` ` sequence))` ``` ``` `; (display "Permutations")(newline)` `; (display (permutations (list 1 2 3)))(newline)` ``` ``` `; Star exercise 2.40` `; Define a procedure unique-pairs that, given an integer n, generates the sequence of pairs (i,j) with (1 <= j < i <= n). Use unique-pairs to simplify the definition of prime-sum-pairs` `(define (seq start stop)` ` (if (> start stop)` ` (list)` ` (cons start (seq (+ start 1) stop))))` ``` ``` `; (display "(seq 1 3)")(newline)` `; (display (seq 1 3))(newline)` ``` ``` `; unique-pairs n -> [ (i,j) | 1<=i