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.1.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-data (lod) is either
5 ;1. empty or
6 ;2. (cons d lod) where d is a Scheme datum and lod is a list-of-data.
7 ;
8 ;replace-eol-with : list-of-data list-of-data -> list-of-data
9 ;Given lod1 and lod2, append lod2 to the end of lod1.
11 (define (replace-eol-with lod1 lod2)
12 (cond
13 [(empty? lod1) lod2]
14 [(cons? lod1) (cons (first lod1) (replace-eol-with (rest lod1) lod2))]))
15 ;Tests
16 ;(define LIST-1 '(happy birthday to you now that you are 21))
17 ;(define LIST-2 '(15 and 13 equal 28))
18 ;(replace-eol-with LIST-1 LIST-2)
20 ;our-append : lod lod lod -> lod
21 ;Given lod1, lod2, and lod3, append lod3 to lod2, and append this resulting list to lod1.
23 (define (our-append lod1 lod2 lod3)
24 (replace-eol-with lod1 (replace-eol-with lod2 lod3)))
26 (define LIST1 '(I am))
27 (define LIST2 '(6 years old))
28 (define LIST3 '(on 3 1 2009))
29 (our-append LIST1 LIST2 LIST3)
31 ;A list-of-symbols (los) is either
32 ;1. empty or
33 ;2. (cons s los) where s is a symbol and los is a list-of-symbols (los).
34 ;
35 ;A list-of-numbers (lon) is either
36 ;1. empty or
37 ;2. (cons n lon) where n is a number and lon is a list-of-numbers (lon).
38 ;
39 ;cross : los lon -> lod
40 ;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 ;
42 ;Example
43 ;(cross '(a b c) '(1 2))
44 ;'((a 1)
45 ; (a 2)
46 ; (b 1)
47 ; (b 2)
48 ; (c 1)
49 ; (c 2))
51 (define (cross a-los a-lon)
52 (cond
53 [(empty? a-los) empty]
54 [(cons? a-los) (append (produce-list (first a-los) a-lon)
55 (cross (rest a-los) a-lon))]))
57 ;produce-list : symbol lon -> list of lod
58 ;Given a-symbol and a-lon, produce a list where a-symbol is matched with
59 ;every number in a-lon to form a list of two-element lists.
61 (define (produce-list a-symbol a-lon)
62 (cond
63 [(empty? a-lon) empty]
64 [(cons? a-lon) (append (list (list a-symbol (first a-lon)))
65 (produce-list a-symbol (rest a-lon)))]))
67 (define LIST-A '(a b c d e))
68 (define LIST-B '(5 6 7 8))
69 (cross LIST-A LIST-B)