-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw4.rkt
81 lines (59 loc) · 2.09 KB
/
hw4.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#lang racket
(provide (all-defined-out)) ;; so we can put tests in a second file
(define (sequence low high stride)
(if (> low high)
null
(cons low (sequence (+ low stride) high stride))))
(define (string-append-map xs suffix)
(map (lambda (s) (string-append s suffix)) xs))
(define (list-nth-mod xs n)
(cond
[(< n 0) (error "list-nth-mod: negative number")]
[(empty? xs) (error "list-nth-mod: empty list")]
[#t (car (list-tail xs (remainder n (length xs))))]))
(define (stream-for-n-steps s n)
(define se (s))
(if (= n 0)
null
(cons (car se) (stream-for-n-steps (cdr se) (- n 1)))))
(define funny-number-stream
(letrec ([f (lambda (x) (cons (if (= (remainder x 5) 0) (- x) x)
(lambda () (f (+ x 1)))))])
(lambda () (f 1))))
(define dan-then-dog
(letrec ([next-string (lambda (s) (if (equal? s "dan.jpg") "dog.jpg" "dan.jpg"))]
[f (lambda (x) (cons x (lambda () (f (next-string x)))))])
(lambda () (f "dan.jpg"))))
(define (stream-add-zero s)
(letrec ([f (lambda (s) (cons (cons 0 (car (s)))
(lambda () (f (cdr (s))))))])
(lambda () (f s))))
(define (cycle-lists xs ys)
(letrec ([generate-nth (lambda (n) (cons (list-nth-mod xs n) (list-nth-mod ys n)))]
[f (lambda (n) (cons (generate-nth n) (lambda () (f (+ n 1)))))])
(lambda () (f 0))))
(define (vector-assoc v vec)
(define (search v vec i)
(if (>= i (vector-length vec))
#f
(let ([ith-elem (vector-ref vec i)])
(if (or (not (pair? ith-elem))
(not (equal? (car ith-elem) v)))
(search v vec (+ 1 i))
ith-elem))))
(search v vec 0))
(define (cached-assoc xs n)
(define cache (make-vector n #f))
(define slot 0)
(lambda (v)
(define cache-hit (vector-assoc v cache))
(if cache-hit
cache-hit
(let
([result (assoc v xs)])
(if result
(begin
[vector-set! cache slot result]
[set! slot (remainder (+ slot 1) n)]
result)
result)))))