这是令牌功能的基本实现:
fun f c = c = #" ";
val testStr = "int main(){return 42;}";
val stringL = String.tokens f testStr;
返回:
val stringL = ["int","main(){return","42;}"] : string list
如何让它返回:
val stringL = ["int"," ","main(){return"," ","42;}"] : string list
我尝试通过管道转发该值,以便它也返回“”。但我只收到错误。
好吧,知道列表中每个元素之间都会出现空格,您可以稍后将它们添加回来。
fun intersperse(_, []) = []
| intersperse(_, lst as [_]) = lst
| intersperse(v, x::xs) =
x :: v :: intersperse(v, xs)
如果您想标记并保留分隔符(例如,可能不止一个空格),那么您将不得不超越
String.tokens
。