-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sml
70 lines (57 loc) · 1.46 KB
/
utils.sml
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
58
59
60
61
62
63
64
65
66
67
68
69
70
structure utils :> utils =
struct
fun listToString l toString =
let
fun listToString' [] _ = []
| listToString' ((x :: xs) : 'a list) (toString : 'a -> string) = (toString x) :: (listToString' xs toString)
val l' = listToString' l toString
in
"["^(String.concatWith "," l')^"]"
end
fun setMap f set compare =
let
val setToList = Splayset.listItems set
val mappedSetToList = List.map f setToList
val emptySet = Splayset.empty(compare)
in
Splayset.addList(emptySet, mappedSetToList)
end
fun setToString s toString =
let
val sToList = Splayset.listItems s
fun setToString' [] _ = []
| setToString' ((x :: xs) : 'a list) (toString : 'a -> string) = (toString x) :: (setToString' xs toString)
val s' = setToString' sToList toString
in
"{"^(String.concatWith "," s')^"}"
end
fun intToString i = if i < 0
then
"-"^Int.toString(~i)
else
Int.toString(i)
fun pairToString (a, b) aToString bToString =
let
val aStr = aToString a
val bStr = bToString b
in
String.concat [
"(",
aStr,
",",
bStr,
")"
]
end
fun id x = x
fun dictToString dict aToString bToString =
let
val dictList = Splaymap.listItems dict
in
listToString dictList (fn pair =>
pairToString pair aToString bToString)
end
fun deleteEnterFromString str =
String.substring(str, 0, (String.size str) - 1)
handle Subscript => raise Fail "Error - deleteEnterFromString()"
end