-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairs.rkt
65 lines (49 loc) · 1.64 KB
/
pairs.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#lang racket
(require "atom.rkt" "set.rkt")
(provide a-pair? first second build-pair set-of-pairs? reverse-pair seconds shift)
(define (a-pair? things)
(and (not (atom? things))
(not (null? things))
(not (null? (cdr things)))
(null? (cdr (cdr things)))))
(define (first pair)
(car pair))
(define (second pair)
(car (cdr pair)))
(define (build-pair one two)
(cons one (cons two (list))))
(define (set-of-pairs? pairs)
(cond
[(null? pairs) #t]
[else (and (a-pair? (car pairs))
(set? pairs)
(set-of-pairs? (cdr pairs)))]))
(define (reverse-pair pair)
(build-pair (second pair) (first pair)))
(define (seconds pairs)
(cond
[(null? pairs) (list)]
[else (cons (second (car pairs)) (seconds (cdr pairs)))]))
(define (shift pair)
(build-pair (first (first pair))
(build-pair (second (first pair)) (second pair))))
(define (align pair-or-atom)
(cond
[(atom? pair-or-atom) pair-or-atom]
[(a-pair? (first pair-or-atom)) (align (shift pair-or-atom))]
[else (build-pair (first pair-or-atom) (align (second pair-or-atom)))]))
(define (length* pair-or-atom)
(cond
[(atom? pair-or-atom) 1]
[else (+ (length* (first pair-or-atom))
(length* (second pair-or-atom)))]))
(define (weight* pair-or-atom)
(cond
[(atom? pair-or-atom) 1]
[else (+ (* (weight* (first pair-or-atom)) 2)
(weight* (second pair-or-atom)))]))
(define (shuffle pair-or-atom)
(cond
[(atom? pair-or-atom) pair-or-atom]
[(a-pair? (first pair-or-atom)) (shuffle (reverse-pair pair-or-atom))]
[else (build-pair (first pair-or-atom) (shuffle (second pair-or-atom)))]))