4 kyu
Simple Syntax Tokenizing
81 of 197AJFarmar
Description:
In this kata, you must create a function, called tokenise
, which takes a string and turns it into a list of tokens. For example:
data Token = Token String | Brackets [Token]
tokenise :: String -> Maybe [Token]
tokenise "A + B * C" = Just [Token "A", Token "+", Token "B", Token "*", Token "C"]
tokenise "function a(arg, arg)" = Just [Token "function", Token "a", Brackets [Token "arg", Token ",", Token "arg"]]
This function does not reduce, and is completely ignorant of context. It simply splits up a string, as a sort of parsing precursor. However, it does understand parentheses. To demonstrate:
tokenise ", () +&" = Just [Token ",", Brackets [], Token "+&"]
tokenise "Mismatched bracket )" = Nothing
A token can be either a series of alphabetical characters or a string whose characters are all one of !#$%&*+-/<=>@^_.,;
. These can be thought of as 'identifiers' and 'operators'. Tokens are seperated either by character type or whitespace. For example:
tokenise "i++" = Just [Token "i", Token "++"]
tokenise "a b@c" = Just [Token "a", Token "b", Token "@", Token "c"]
More examples are given in the example test cases.
(Any translations would be much appreciated.)
Strings
Parsing
Algorithms
Similar Kata:
Stats:
Created | Aug 20, 2017 |
Published | Aug 20, 2017 |
Warriors Trained | 535 |
Total Skips | 78 |
Total Code Submissions | 1733 |
Total Times Completed | 197 |
Haskell Completions | 81 |
JavaScript Completions | 120 |
Total Stars | 46 |
% of votes with a positive feedback rating | 95% of 75 |
Total "Very Satisfied" Votes | 68 |
Total "Somewhat Satisfied" Votes | 6 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 8 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 6 kyu |