mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-12 01:05:32 +01:00
49 lines
1.2 KiB
Plaintext
49 lines
1.2 KiB
Plaintext
(def! char (fn* [l]
|
|
(car (boom l))))
|
|
|
|
(def! #n (char "\n"))
|
|
(def! #s (char " "))
|
|
(def! #t (char "\t"))
|
|
|
|
(def! char? (fn* [a]
|
|
(= (type a) :char)))
|
|
|
|
(def! string? (fn* [a]
|
|
(= (type a) :string)))
|
|
|
|
(def! strlen (fn* [s] (count (boom s))))
|
|
|
|
(def! strc (fn* [l]
|
|
(def! strc-r (fn* [l s]
|
|
(if (empty? l)
|
|
s
|
|
(strc-r (cdr l) (str s (car l))))))
|
|
(strc-r l "")))
|
|
|
|
(def! split (fn* [s c]
|
|
"Split the string at every occurrence of character sc"
|
|
(if (and (string? s)
|
|
(char? c))
|
|
(def! split-r (fn* [l t r]
|
|
(if (empty? l)
|
|
(cons t r)
|
|
(do (def! cc (car l))
|
|
(if (= cc c)
|
|
(split-r (cdr l) "" (cons t r))
|
|
(split-r (cdr l) (str t cc) r))))))
|
|
(raise "split: accepts a string and a char as arguments"))
|
|
(reverse (split-r (boom s) "" '()))))
|
|
|
|
(def! join (fn* [l s]
|
|
"Join element of list l to a stiring, using s as separator"
|
|
(def! join-r (fn* [l t]
|
|
(if (empty? l)
|
|
t
|
|
(join-r (cdr l) (str t s (car l))))))
|
|
(join-r (cdr l) (car l))))
|
|
|
|
(def! chsub (fn* [s c1 c2]
|
|
(strc (map-if (fn* [x] (= x c1)) (fn* [x] c2) (boom s)))))
|
|
|
|
|