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.3.0) (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-symbols is either
5 12687dd9 2023-08-04 jrmu ;1. empty or
6 12687dd9 2023-08-04 jrmu ;2. (cons s los)
7 12687dd9 2023-08-04 jrmu ;where s is a symbol and los is a list-of-symbols.
8 12687dd9 2023-08-04 jrmu ;
9 12687dd9 2023-08-04 jrmu ;A natural-number [>=1] (N[>=1]) is either
10 12687dd9 2023-08-04 jrmu ;1. 1 or
11 12687dd9 2023-08-04 jrmu ;2. (add1 n) where n is [N(>=1)]
12 12687dd9 2023-08-04 jrmu
13 12687dd9 2023-08-04 jrmu ;list-pick : list-of-symbols N[>=1] -> symbol
14 12687dd9 2023-08-04 jrmu ;Given a-los and n, find the nth symbol in a-los. The first symbol in a-los has an index of 1.
15 12687dd9 2023-08-04 jrmu
16 12687dd9 2023-08-04 jrmu (define (list-pick a-los n)
17 12687dd9 2023-08-04 jrmu (cond
18 12687dd9 2023-08-04 jrmu [(and (= n 1)
19 12687dd9 2023-08-04 jrmu (empty? a-los)) (error 'list-pick "list too short")]
20 12687dd9 2023-08-04 jrmu [(and (= n 1)
21 12687dd9 2023-08-04 jrmu (cons? a-los)) (first a-los)]
22 12687dd9 2023-08-04 jrmu [(and (> n 1)
23 12687dd9 2023-08-04 jrmu (empty? a-los)) (error 'list-pick "list too short")]
24 12687dd9 2023-08-04 jrmu [(and (> n 1)
25 12687dd9 2023-08-04 jrmu (cons? a-los)) (list-pick (rest a-los) (sub1 n))]))
26 12687dd9 2023-08-04 jrmu
27 12687dd9 2023-08-04 jrmu (define los '(Fred Ted Bill Bob Joe))
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu ;A natural-number [>=0] (N[>=0]) is either
30 12687dd9 2023-08-04 jrmu ;1. 0 or
31 12687dd9 2023-08-04 jrmu ;2. (add1 n) where n is [N(>=0)].
32 12687dd9 2023-08-04 jrmu
33 12687dd9 2023-08-04 jrmu ;list-pick : list-of-symbols N[>=1] -> symbol
34 12687dd9 2023-08-04 jrmu ;Given a-los and n, find the nth symbol in a-los. The first symbol in a-los has an index of 0.
35 12687dd9 2023-08-04 jrmu ;(define (list-pick0 a-los n)
36 12687dd9 2023-08-04 jrmu ; (cond
37 12687dd9 2023-08-04 jrmu ; [(and (= n 0)
38 12687dd9 2023-08-04 jrmu ; (empty? a-los)) (error 'list-pick0 "list too short")]
39 12687dd9 2023-08-04 jrmu ; [(and (= n 0)
40 12687dd9 2023-08-04 jrmu ; (cons? a-los)) (first a-los)]
41 12687dd9 2023-08-04 jrmu ; [(and (> n 0)
42 12687dd9 2023-08-04 jrmu ; (empty? a-los)) (error 'list-pick0 "list too short")]
43 12687dd9 2023-08-04 jrmu ; [(and (> n 0)
44 12687dd9 2023-08-04 jrmu ; (cons? a-los)) (list-pick0 (rest a-los) (sub1 n))]))
45 12687dd9 2023-08-04 jrmu (define (list-pick0 a-los n)
46 12687dd9 2023-08-04 jrmu (cond
47 12687dd9 2023-08-04 jrmu [(empty? a-los) (error 'list-pick0 "list too short")]
48 12687dd9 2023-08-04 jrmu [(= n 0) (first a-los)]
49 12687dd9 2023-08-04 jrmu [(> n 0) (list-pick0 (rest a-los) (sub1 n))]))
50 12687dd9 2023-08-04 jrmu
51 12687dd9 2023-08-04 jrmu
52 12687dd9 2023-08-04 jrmu ;
53 12687dd9 2023-08-04 jrmu ;Data Definition
54 12687dd9 2023-08-04 jrmu ;A list-of-data is either
55 12687dd9 2023-08-04 jrmu ;1. empty or
56 12687dd9 2023-08-04 jrmu ;2. (cons d lod)
57 12687dd9 2023-08-04 jrmu ;where d is a Scheme datum and lod is a list-of-data.
58 12687dd9 2023-08-04 jrmu ;
59 12687dd9 2023-08-04 jrmu ;replace-eol-with : list-of-data list-of-data -> list-of-data
60 12687dd9 2023-08-04 jrmu ;(define (replace-eol-with lod1 lod2)
61 12687dd9 2023-08-04 jrmu ; (cond
62 12687dd9 2023-08-04 jrmu ; [(empty? lod1)
63 12687dd9 2023-08-04 jrmu ; (cond
64 12687dd9 2023-08-04 jrmu ; [(empty? lod2) empty]
65 12687dd9 2023-08-04 jrmu ; [(cons? lod2) lod2])]
66 12687dd9 2023-08-04 jrmu ; [(cons? lod1)
67 12687dd9 2023-08-04 jrmu ; (cond
68 12687dd9 2023-08-04 jrmu ; [(empty? lod2) lod1]
69 12687dd9 2023-08-04 jrmu ; [(cons? lod2) (append (list (first lod1))
70 12687dd9 2023-08-04 jrmu ; (replace-eol-with (rest lod1) lod2))])]))
71 12687dd9 2023-08-04 jrmu
72 12687dd9 2023-08-04 jrmu (define (replace-eol-with lod1 lod2)
73 12687dd9 2023-08-04 jrmu (cond
74 12687dd9 2023-08-04 jrmu [(and (empty? lod1)
75 12687dd9 2023-08-04 jrmu (empty? lod2)) empty]
76 12687dd9 2023-08-04 jrmu [(and (empty? lod1)
77 12687dd9 2023-08-04 jrmu (cons? lod2)) lod2]
78 12687dd9 2023-08-04 jrmu [(empty? lod2) lod1]
79 12687dd9 2023-08-04 jrmu [(cons? lod2) (append (list (first lod1))
80 12687dd9 2023-08-04 jrmu (replace-eol-with (rest lod1) lod2))]))
81 12687dd9 2023-08-04 jrmu