如何用switch
operator 而不是 large实现这个逻辑if/else
?
type token =
| DOT
| OPEN_BRACKET
| SYMBOL;
let dot_code = ".".[0] |> Char.code;
let open_bracket_code = "{".[0] |> Char.code;
let char_to_token symbol :token => {
let code = Char.code symbol;
let c =
if (code === dot_code) {
DOT
} else if (code === open_bracket_code) {
OPEN_BRACKET
} else {
SYMBOL
};
c
};
String.iter
(
fun symbol => {
let c = char_to_token symbol;
switch c {
| DOT => print_string " DOT "
| OPEN_BRACKET => print_string " OPEN_BRACKET "
| SYMBOL => print_char symbol
}
}
)
"a.s {";
.
或者迭代字符串和检测符号的最佳方法是什么{
?