Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #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 12687dd9 2023-08-04 jrmu ;arrangements : list-of-names -> list-of-list-of-names
5 12687dd9 2023-08-04 jrmu ;Given names (a list-of-names), return all permutations of the names.
6 12687dd9 2023-08-04 jrmu
7 12687dd9 2023-08-04 jrmu ;insert-in-lolon : symbol list-of-list-of-names -> list-of-list-of-names
8 12687dd9 2023-08-04 jrmu ;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.
9 12687dd9 2023-08-04 jrmu
10 12687dd9 2023-08-04 jrmu ;insert-name : symbol list-of-names N[>=0] -> list-of-list-of-names
11 12687dd9 2023-08-04 jrmu ;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.
12 12687dd9 2023-08-04 jrmu
13 12687dd9 2023-08-04 jrmu ;insert-name-at-posn : symbol list-of-names N[>=0] -> list-of-names
14 12687dd9 2023-08-04 jrmu ;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 12687dd9 2023-08-04 jrmu (define (arrangements names)
16 12687dd9 2023-08-04 jrmu (local ((define (arrangements names)
17 12687dd9 2023-08-04 jrmu (cond
18 12687dd9 2023-08-04 jrmu [(empty? names) (list empty)]
19 12687dd9 2023-08-04 jrmu [else (insert-in-lolon (first names) (arrangements (rest names)))]))
20 12687dd9 2023-08-04 jrmu
21 12687dd9 2023-08-04 jrmu
22 12687dd9 2023-08-04 jrmu (define (insert-in-lolon a-symbol a-lolon)
23 12687dd9 2023-08-04 jrmu (cond
24 12687dd9 2023-08-04 jrmu [(empty? a-lolon) empty]
25 12687dd9 2023-08-04 jrmu [(cons? a-lolon)
26 12687dd9 2023-08-04 jrmu (append (insert-name a-symbol (first a-lolon) (length (first a-lolon)))
27 12687dd9 2023-08-04 jrmu (insert-in-lolon a-symbol (rest a-lolon)))]))
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu
30 12687dd9 2023-08-04 jrmu (define (insert-name a-symbol names n)
31 12687dd9 2023-08-04 jrmu (cond
32 12687dd9 2023-08-04 jrmu [(= n 0) (list (insert-name-at-posn a-symbol names n))]
33 12687dd9 2023-08-04 jrmu [(cons? names) (append (list (insert-name-at-posn a-symbol names n))
34 12687dd9 2023-08-04 jrmu (insert-name a-symbol names (sub1 n)))]
35 12687dd9 2023-08-04 jrmu [(empty? names) (error 'insert-name "unexpected error")]))
36 12687dd9 2023-08-04 jrmu
37 12687dd9 2023-08-04 jrmu (define (insert-name-at-posn a-symbol names n)
38 12687dd9 2023-08-04 jrmu (cond
39 12687dd9 2023-08-04 jrmu [(= n 0) (append (list a-symbol) names)]
40 12687dd9 2023-08-04 jrmu [(cons? names) (append (list (first names))
41 12687dd9 2023-08-04 jrmu (insert-name-at-posn a-symbol (rest names) (sub1 n)))]
42 12687dd9 2023-08-04 jrmu [(empty? names) (error 'insert-name-at-posn "list too short")])))
43 12687dd9 2023-08-04 jrmu (arrangements names)))