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 17.2.1) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "dir.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "dir.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;A list-of-data (lod) is either
5 12687dd9 2023-08-04 jrmu ;1. empty or
6 12687dd9 2023-08-04 jrmu ;2. (cons d lod) where d is a Scheme datum and lod is a list-of-data.
7 12687dd9 2023-08-04 jrmu ;
8 12687dd9 2023-08-04 jrmu ;replace-eol-with : list-of-data list-of-data -> list-of-data
9 12687dd9 2023-08-04 jrmu ;Given lod1 and lod2, append lod2 to the end of lod1.
10 12687dd9 2023-08-04 jrmu
11 12687dd9 2023-08-04 jrmu (define (replace-eol-with lod1 lod2)
12 12687dd9 2023-08-04 jrmu (cond
13 12687dd9 2023-08-04 jrmu [(empty? lod1) lod2]
14 12687dd9 2023-08-04 jrmu [(cons? lod1) (cons (first lod1) (replace-eol-with (rest lod1) lod2))]))
15 12687dd9 2023-08-04 jrmu ;Tests
16 12687dd9 2023-08-04 jrmu ;(define LIST-1 '(happy birthday to you now that you are 21))
17 12687dd9 2023-08-04 jrmu ;(define LIST-2 '(15 and 13 equal 28))
18 12687dd9 2023-08-04 jrmu ;(replace-eol-with LIST-1 LIST-2)
19 12687dd9 2023-08-04 jrmu
20 12687dd9 2023-08-04 jrmu ;our-append : lod lod lod -> lod
21 12687dd9 2023-08-04 jrmu ;Given lod1, lod2, and lod3, append lod3 to lod2, and append this resulting list to lod1.
22 12687dd9 2023-08-04 jrmu
23 12687dd9 2023-08-04 jrmu (define (our-append lod1 lod2 lod3)
24 12687dd9 2023-08-04 jrmu (replace-eol-with lod1 (replace-eol-with lod2 lod3)))
25 12687dd9 2023-08-04 jrmu
26 12687dd9 2023-08-04 jrmu (define LIST1 '(I am))
27 12687dd9 2023-08-04 jrmu (define LIST2 '(6 years old))
28 12687dd9 2023-08-04 jrmu (define LIST3 '(on 3 1 2009))
29 12687dd9 2023-08-04 jrmu (our-append LIST1 LIST2 LIST3)
30 12687dd9 2023-08-04 jrmu
31 12687dd9 2023-08-04 jrmu ;A list-of-symbols (los) is either
32 12687dd9 2023-08-04 jrmu ;1. empty or
33 12687dd9 2023-08-04 jrmu ;2. (cons s los) where s is a symbol and los is a list-of-symbols (los).
34 12687dd9 2023-08-04 jrmu ;
35 12687dd9 2023-08-04 jrmu ;A list-of-numbers (lon) is either
36 12687dd9 2023-08-04 jrmu ;1. empty or
37 12687dd9 2023-08-04 jrmu ;2. (cons n lon) where n is a number and lon is a list-of-numbers (lon).
38 12687dd9 2023-08-04 jrmu ;
39 12687dd9 2023-08-04 jrmu ;cross : los lon -> lod
40 12687dd9 2023-08-04 jrmu ;Given a-los and a-lon, find all possible cross combinations of a single symbol with a single number (a list-of-data with 2 elements). Return the possible combinations as a list of list-of-data.
41 12687dd9 2023-08-04 jrmu ;
42 12687dd9 2023-08-04 jrmu ;Example
43 12687dd9 2023-08-04 jrmu ;(cross '(a b c) '(1 2))
44 12687dd9 2023-08-04 jrmu ;'((a 1)
45 12687dd9 2023-08-04 jrmu ; (a 2)
46 12687dd9 2023-08-04 jrmu ; (b 1)
47 12687dd9 2023-08-04 jrmu ; (b 2)
48 12687dd9 2023-08-04 jrmu ; (c 1)
49 12687dd9 2023-08-04 jrmu ; (c 2))
50 12687dd9 2023-08-04 jrmu
51 12687dd9 2023-08-04 jrmu (define (cross a-los a-lon)
52 12687dd9 2023-08-04 jrmu (cond
53 12687dd9 2023-08-04 jrmu [(empty? a-los) empty]
54 12687dd9 2023-08-04 jrmu [(cons? a-los) (append (produce-list (first a-los) a-lon)
55 12687dd9 2023-08-04 jrmu (cross (rest a-los) a-lon))]))
56 12687dd9 2023-08-04 jrmu
57 12687dd9 2023-08-04 jrmu ;produce-list : symbol lon -> list of lod
58 12687dd9 2023-08-04 jrmu ;Given a-symbol and a-lon, produce a list where a-symbol is matched with
59 12687dd9 2023-08-04 jrmu ;every number in a-lon to form a list of two-element lists.
60 12687dd9 2023-08-04 jrmu
61 12687dd9 2023-08-04 jrmu (define (produce-list a-symbol a-lon)
62 12687dd9 2023-08-04 jrmu (cond
63 12687dd9 2023-08-04 jrmu [(empty? a-lon) empty]
64 12687dd9 2023-08-04 jrmu [(cons? a-lon) (append (list (list a-symbol (first a-lon)))
65 12687dd9 2023-08-04 jrmu (produce-list a-symbol (rest a-lon)))]))
66 12687dd9 2023-08-04 jrmu
67 12687dd9 2023-08-04 jrmu (define LIST-A '(a b c d e))
68 12687dd9 2023-08-04 jrmu (define LIST-B '(5 6 7 8))
69 12687dd9 2023-08-04 jrmu (cross LIST-A LIST-B)