-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstar.rkt
61 lines (50 loc) · 2.05 KB
/
star.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
#lang racket
(require "atom.rkt" "eqan.rkt")
(provide remove-member* insert-after* occur* substitute* insert-before* member*)
(define (remove-member* a things)
(cond
[(null? things) (list)]
[(atom? (car things))
(cond
[(eq? a (car things)) (remove-member* a (cdr things))]
[else (cons (car things) (remove-member* a (cdr things)))])
]
[else (cons (remove-member* a (car things)) (remove-member* a (cdr things)))]))
(define (insert-after* new old things)
(cond
[(null? things) (list)]
[(atom? (car things))
(cond
[(eq? old (car things)) (cons old (cons new (insert-after* new old (cdr things))))]
[else (cons (car things) (insert-after* new old (cdr things)))])]
[else (cons (insert-after* new old (car things)) (insert-after* new old (cdr things)))]))
(define (occur* value things)
(cond
[(null? things) 0]
[(atom? (car things))
(cond
[(eqan? value (car things)) (add1 (occur* value (cdr things)))]
[else (occur* value (cdr things))])]
[else (+ (occur* value (car things (occur* value (cdr things)))))]))
(define (substitute* old new things)
(cond
[(null? things) (list)]
[(atom? (car things))
(cond
[(eqan? old (car things)) (cons new (substitute* new old (cdr things)))]
[else (cons (car things) (substitute* new old (cdr things)))])]
[else (cons (substitute* new old (car things)) (substitute* new old (cdr things)))]))
(define (insert-before* new old things)
(cond
[(null? things) (list)]
[(atom? (car things))
(cond
[(eqan? old (car things)) (cons new (cons old (insert-before* old new (cdr things))))]
[else (cons (car things) (insert-before* old new (cdr things)))])]
[else (cons (insert-before* old new (car things)) (insert-before* old new (cdr things)))]))
(define (member* value things)
(cond
[(null? things) #f]
[(atom? (car things))
(or (eqan? value (car things)) (member* value (cdr things)))]
[else (or (member* value (car things)) (member* value (cdr things)))]))