-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.u
30 lines (29 loc) · 775 Bytes
/
str.u
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
(def isDigit (\x (and (>= x '0') (<= x '9'))))
(def isSpace (\x (or (= x ' ') (or (= x '\t') (= x '\n')))))
(def intFromDigit (\x (- x '0')))
(def digitFromInt (+ '0'))
(def atoi (\s (let
(atoiP (foldl (\t a (+ (* t 10) (intFromDigit a))) 0 ))
(s 0 (\h r (
(= h '-') (neg (atoiP r))
(atoiP s)
))))
))
(def itoa (\s (let
(itoaP (recur (\f (\tail x
((<= x 9) (cons (digitFromInt x) tail)
(f (cons (digitFromInt (% x 10)) tail) (/ x 10))
))) ""))
((>= s 0) (itoaP s)
(cons '-' (itoaP (neg s)))
))
))
(def splitStr (\d s (let
(appendHead (\a l (l (list (list a)) (\h r (cons (cons a h) r)))))
(foldr (\a r
((= a d) (cons empty r)
(appendHead a r)
)
) empty s)
)))
(def rstrip (foldr (\a r (or (not (isSpace a)) (not (null r)) (cons a r) r)) empty))