Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 18.1.9) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp")))))
4 ;arrangements : list-of-names -> list-of-list-of-names
5 ;Given names (a list-of-names), return all permutations of the names.
7 ;insert-in-lolon : symbol list-of-list-of-names -> list-of-list-of-names
8 ;Given a-symbol and a-lolon, insert a-symbol into every position in each list-of-names element within a-lolon to return a new list-of-list-of-names.
10 ;insert-name : symbol list-of-names N[>=0] -> list-of-list-of-names
11 ;Given a-symbol, insert it into every position in names starting at the n-th position, continuing to the very beginning of the list-of-names (n=0), to return a list-of-list-of-names.
13 ;insert-name-at-posn : symbol list-of-names N[>=0] -> list-of-names
14 ;Given a-symbol (symbol), names (list-of-names), and n (N[>=0]), insert a-symbol into names at the n-th position and return the list-of-names.
15 (define (arrangements names)
16 (local ((define (arrangements names)
17 (cond
18 [(empty? names) (list empty)]
19 [else (insert-in-lolon (first names) (arrangements (rest names)))]))
22 (define (insert-in-lolon a-symbol a-lolon)
23 (cond
24 [(empty? a-lolon) empty]
25 [(cons? a-lolon)
26 (append (insert-name a-symbol (first a-lolon) (length (first a-lolon)))
27 (insert-in-lolon a-symbol (rest a-lolon)))]))
30 (define (insert-name a-symbol names n)
31 (cond
32 [(= n 0) (list (insert-name-at-posn a-symbol names n))]
33 [(cons? names) (append (list (insert-name-at-posn a-symbol names n))
34 (insert-name a-symbol names (sub1 n)))]
35 [(empty? names) (error 'insert-name "unexpected error")]))
37 (define (insert-name-at-posn a-symbol names n)
38 (cond
39 [(= n 0) (append (list a-symbol) names)]
40 [(cons? names) (append (list (first names))
41 (insert-name-at-posn a-symbol (rest names) (sub1 n)))]
42 [(empty? names) (error 'insert-name-at-posn "list too short")])))
43 (arrangements names)))