-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstrings.hs
58 lines (40 loc) · 1.37 KB
/
strings.hs
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
main :: IO ()
main = Ret ()
get :: String -> Integer -> Integer
get s idx = #(__Elem) s idx
index :: String -> Integer -> Maybe Integer
index s idx = if idx < 0 then Nothing
else if length s < idx + 1 then Nothing
else Just (get s idx)
length :: String -> Integer
length s = #(__Len) s
concat :: String -> String -> String
concat s1 s2 = #(__Concat) s1 s2
substring2 :: String -> Integer -> String
substring2 s start = #(__Substring) s start
substring3 :: String -> Integer -> Integer -> String
substring3 s start len = #(__Substring) s start len
equal :: String -> String -> Bool
equal s1 s2 = #(__StrEq) s1 s2
less :: String -> String -> Bool
less s1 s2 = #(__StrLt) s1 s2
greater :: String -> String -> Bool
greater s1 s2 = #(__StrGt) s1 s2
leq :: String -> String -> Bool
leq s1 s2 = #(__StrLeq) s1 s2
geq :: String -> String -> Bool
geq s1 s2 = #(__StrGeq) s1 s2
implode :: [Integer] -> String
implode l = case l of [] -> ""
h:t -> concat (#(__Implode) h) (implode t)
explode :: String -> [Integer]
explode s =
let from i = if i < length s then get s i : from (i + 1) else []
in from 0
reverse :: String -> String
reverse s =
let revAux i limit acc = if i == limit then acc else
revAux (i + 1) limit ((get s i) : acc)
in implode (revAux 0 (length s) [])
-- Helpers
data Maybe a = Nothing | Just a