-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.scm
57 lines (50 loc) · 2.25 KB
/
parse.scm
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
(import (chicken string))
(import (chicken condition))
(define (check-is-approved number approved-numbers)
(cond ((equal? (length approved-numbers) 0)
#f)
((equal? (car approved-numbers) number)
#t)
(else (check-is-approved number (cdr approved-numbers)))))
(define (convert-to-approved-numbers string-list approved-numbers)
(let convert-to-approved-loop ((number-list (list))
(string-list string-list)
(approved-numbers approved-numbers))
(if (equal? (length string-list) 0)
number-list
(let ((number (string->number (car string-list))))
(if (check-is-approved number approved-numbers)
(convert-to-approved-loop (append number-list (list number)) (cdr string-list) approved-numbers)
(abort "must use integers 1:10 inclusive"))))))
(define (parse-small-arg arg)
(define approved-small-numbers (list 1 2 3 4 5 6 7 8 9 10))
(convert-to-approved-numbers (string-split arg ",") approved-small-numbers))
(define (parse-large-arg arg)
(define approved-large-numbers (list 25 50 75 100))
(convert-to-approved-numbers (string-split arg ",") approved-large-numbers))
(define (parse-numeric-arg arg)
(string->number arg))
(define (parse-target-arg arg)
(define min 100)
(define max 999)
(define target (parse-numeric-arg arg))
(if (or (< target min) (> target max))
(abort "Target is out of approved range")
target))
(define (expr->string expression)
(let to-string-loop ((str "")
(expression expression))
(define to-add "")
(if (null? expression)
str
(let ((symbol (car expression)))
(cond ((equal? symbol +)
(set! to-add "+"))
((equal? symbol -)
(set! to-add "-"))
((equal? symbol *)
(set! to-add "*"))
((equal? symbol /)
(set! to-add "/"))
(else (set! to-add (number->string symbol))))
(to-string-loop (conc (conc str to-add) " ") (cdr expression))))))