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 19.2.3) (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 (define-struct pairs (left right))
6 ;Pairs are a structure
7 ;(make-pairs l r)
8 ;where l is an X and r is a Y.
9 ;
10 ;Some concrete examples of this abstract class of data are:
11 ;(listof (make-pairs number number))
12 ;(listof (make-pairs 5 6))
13 ;(listof (make-pairs symbol number))
14 ;(listof (make-pairs 'words 5))
15 ;(listof (make-pairs symbol symbol))
16 ;(listof (make-pairs 'leftside 'rightside))
18 ;lefts : (listof (make-pairs X Y)) -> (listof X)
19 ;Given alopxy, return only the left side of the pairs as a (listof X).
21 (define (lefts alopxy)
22 (cond
23 [(empty? alopxy) empty]
24 [(cons? alopxy) (cons (pairs-left (first alopxy)) (lefts (rest alopxy)))]))
26 (define list1 (list (make-pairs 5 6)
27 (make-pairs 'false false)
28 (make-pairs true 'heyThere)
29 (make-pairs (make-pairs 5 6) (make-pairs 'hey 'me))
30 (make-pairs '(5 1 4 2) '(hey joe its me))
31 (make-pairs 1 'word)
32 (make-pairs 'last 'one)))
34 ;rights : (listof (make-pairs X Y)) -> (listof X)
35 ;Given alopxy, return only the right side of the pairs as a (listof X).
37 (define (rights alopxy)
38 (cond
39 [(empty? alopxy) empty]
40 [(cons? alopxy) (cons (pairs-right (first alopxy)) (rights (rest alopxy)))]))
42 ;side : ((make-pairs X Y) -> X or Y) (listof (make-pairs X Y)) -> (listof X)
43 ;Given which-side and alopxy, return only one side of the pairs as a (listof X). which-side should be either pairs-left or pairs-right.
45 (define (side which-side alopxy)
46 (cond
47 [(empty? alopxy) empty]
48 [(cons? alopxy) (cons (which-side (first alopxy)) (side which-side (rest alopxy)))]))
50 (equal? (lefts list1) (side pairs-left list1))
51 (equal? (rights list1) (side pairs-right list1))