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