-
Notifications
You must be signed in to change notification settings - Fork 0
/
zpconversion.zp
53 lines (46 loc) · 1.48 KB
/
zpconversion.zp
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
(define (string->integer x)
(let ((num (string->number x)))
(cond
((integer? num) num)
((float? num) (round num))
((rational? num) (round num))
((complex? num) (round (real num)))
(else #f))))
(define (string->float x)
(let ((num (string->number x)))
(cond
((integer? num) (* 1.0 num))
((float? num) num)
((rational? num) (exact->inexact num))
((complex? num) (real num))
(else #f))))
(define (string->rational x)
(let ((num (string->number x)))
(cond
((integer? num) (/ 2 1/1))
((float? num) (round num))
((rational? num) (round num))
((complex? num) (/ (round (real num)) 1/1))
(else #f))))
(define (string->complex x)
(let ((num (string->number x)))
(cond
((integer? num) (* num 1+0i))
((float? num) (* num 1+0i))
((rational? num) (* num 1+0i))
((complex? num) num)
(else #f))))
(define (string->boolean x)
(cond
((string=? x "true") #t)
((string=? x "false") #f)
(else :err)))
(define (name x) "converts symbol or atom to a string"
(cond
((atom? x) (string:tail (symbol->string x)))
((symbol? x) (symbol->string x))
(else #f)))
(define (ascii-string->byte-vector str) "converts ascii string to byte-vector"
(string:reduce (lambda (acc x) (++ acc (char->integer x))) b{} str))
(define (byte-vector->ascii-string b) "converts byte-vector to ascii string"
(byte-vector:reduce (lambda (acc x) (++ acc (integer->char x))) "" b))