-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.rkt
48 lines (41 loc) · 1.6 KB
/
utils.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
#lang racket
(provide (all-defined-out))
(define (list-range lst start end)
(take (drop lst start) (- end start)))
(define (n-times-string a-string n)
(cond
[(< n 1) ""]
[(= n 1) a-string]
[else
(string-append a-string
(n-times-string a-string (sub1 n)))]))
(define (take-up-to n xs)
(cond [(or (zero? n) (empty? xs)) empty]
[else (cons (car xs)
(take-up-to (- n 1) (cdr xs)))]))
(define (drop-up-to to-drop xs)
(cond [(or (= to-drop 0) (empty? xs)) xs]
[else (drop-up-to (sub1 to-drop) (cdr xs))]))
(define (split-into-chunks-of-size-n xs n)
(cond [(empty? xs) empty]
[else (let ([first-chunk (take-up-to n xs)]
[rest (drop-up-to n xs)])
(cons first-chunk (split-into-chunks-of-size-n rest n)))]))
(define (mean lst)
(exact->inexact
(/ (apply + lst)
(length lst))))
(define (maximum-for-procedure a-list proc-prev proc-current)
(define (iter remaining maximum-element)
(cond [(empty? remaining) maximum-element]
[(> (proc-current (first remaining))
(proc-prev maximum-element))
(displayln (string-append "current with tolerance is greater: "
(number->string (proc-current (first remaining)))
" > "
(number->string (proc-prev maximum-element))))
(iter (rest remaining)
(first remaining))]
[else (iter (rest remaining)
maximum-element)]))
(iter (rest a-list) (first a-list)))