-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ss
executable file
Β·155 lines (118 loc) Β· 4.23 KB
/
utils.ss
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env scheme
;; File -> (listof String)
;; produce list where each string is one line in file
(define (file->list f)
(define (file->list ln lst)
(cond [(eq? #!eof ln) (reverse lst)]
[else
(file->list (get-line f) (cons ln lst))]))
(file->list (get-line f) '()))
;; String -> (listof String)
;; produce list where each string is one line from file name
(define (read-file fname)
(file->list (open-input-file fname)))
;; Display string (or number) with newline after it
(define (print s)
(display (string-append (if (number? s) (number->string s) s) "\n")))
(define (identity x) x)
;; String Char -> (listof String)
;; produce list of substrings from str split with tok
(define (string-split str tok)
(define (string-split pos los acc)
(cond [(= pos (string-length str)) (append los (list acc))]
[else
(let [(c (string-ref str pos))]
(if (char=? c tok)
(string-split (add1 pos)
(append los (list acc))
"")
(string-split (add1 pos)
los
(string-append acc (string c)))))]))
(string-split 0 '() ""))
(assert (equal?
(string-split "a,bc,def" #\,)
(list "a" "bc" "def")))
(assert (equal?
(string-split "1:0:0:3:1:1:2" #\:)
(list "1" "0" "0" "3" "1" "1" "2")))
(assert (equal?
(string-split "123 567 789" #\ )
(list "123" "567" "789")))
;; String -> String | (listof T) -> T
;; Returns first element of either string or list
(define (first x)
(cond [(string? x) (if (eq? "" x) "" (string-slice x 0 1))]
[(list? x) (car x)]
[else
(and (display x)
(raise
(condition
(make-error)
(make-message-condition
(string-append
"first called with above ^^;\n"
"should be called with list or string")))))]))
;; String -> String | (listof T) -> (listof T)
;; Returns remaining elements after first of either string or list
(define (rest x)
(cond [(string? x) (if (eq? "" x) "" (string-slice x 1))]
[(list? x) (cdr x)]
[else
(and (display x)
(raise
(condition
(make-error)
(make-message-condition
(string-append
"rest called with above ^^;\n"
"should be called with list or string")))))]))
;; Alias empty list
(define empty '())
;; (listof X) -> Boolean
;; produce true if list empty
(define empty? null?)
(define (false? x)
(equal? #f x))
;; (listof X) Integer X -> (listof X)
;; produce new list from lst such that at pos value is x
;; if pos out of bounds for lst, there's no change to lst
(define (list-set lst pos x)
(cond [(zero? pos) (cons x (rest lst))]
[(empty? lst) empty]
[else
(cons (first lst) (list-set (rest lst) (sub1 pos) x))]))
;; X (listof X) -> (listof X)
;; like cons, but in reverse
(define (snoc x lox)
(reverse (cons x (reverse lox))))
(assert (equal? (snoc 4 (list 1 2 3)) (list 1 2 3 4)))
(assert (equal? (snoc "z" (list "x" "y")) (list "x" "y" "z")))
(define (test a b)
(assert (equal? a b)))
;; Integer -> (listof Integer)
;; produce a range of integers [0, n) (up to n exclusive)
(define range iota)
;; String Number [Number] -> String
;; produce substring of str with start [, end] params
(define (string-slice str start . end)
(substring str start (if (empty? end) (string-length str) (car end))))
;; (listof X) Natural -> (listof X)
;; return list of first n elements in lst
(define (take lst n)
(define (take lst n rsf)
(cond [(empty? lst) rsf]
[(zero? n) rsf]
[else
(take (cdr lst) (sub1 n) (snoc (car lst) rsf))]))
(take lst n '()))
;; X (listof X) -> Natural|False
;; produce index of el in lst, #f if not found
(define (index el lst)
(let [(tail (member el (reverse lst)))]
(and tail (length (cdr tail)))))
;; TODO refactor last to work with strings
(define (last lst)
(car (reverse lst)))
(define (string->program s)
(map string->number (string-split s #\,)))