Created
December 3, 2010 05:01
-
-
Save itsuart/726599 to your computer and use it in GitHub Desktop.
packing tokens with ocaml pattern matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
На входе список токенов, задача - объединить подпоследовательности вида "String, '_', String" и "'-', Number" в одну, т.е. | |
Вход: ["foo", '_', "bar", '-', 12, "some", "other", 15, "baz"] | |
Выход: ["foo_bar", -12, "some", "other", 15, "baz"] | |
*) | |
let pack_tokens tokens = | |
let accum = ref [] in | |
let rec pack tokens_left = match tokens_left with | |
[] -> List.rev !accum | |
| Tokenizer.String l :: Tokenizer.Symbol '_' :: Tokenizer.String r :: tail -> | |
pack (Tokenizer.String (String.concat "_" [l;r]) :: tail) | |
| Tokenizer.Symbol '-' :: Tokenizer.Number n :: tail -> pack (Tokenizer.Number (-n) :: tail) | |
| h :: tail -> accum := h :: !accum; pack tail | |
in pack tokens |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment