Bob*_*ong 3 f# functional-programming
我有这个方法,它接受一个列表并将其转换为字节码字符串.它的工作方式与我期望的一样; 但是,我得到一个我不想要的尾随空间.问题:如何摆脱这最后的0尾随?
Input: byteCode [SC 10; SC 2; SAdd; SC 32; SC 4; SC 5; SAdd; SMul; SAdd]
let rec byteCode (l : sInstr list) : string =
match l with
| [] -> ""
| (SC n :: l) -> "0 " + string n + " " + byteCode l
| (SAdd :: l) -> "1 " + byteCode l
| (SSub :: l) -> "2 " + byteCode l
| (SMul :: l) -> "3 " + byteCode l
| (SNeg :: l) -> "4 " + byteCode l
| (SLess :: l) -> "5 " + byteCode l
| (SIfze n :: l) -> "6 " + string n + " " + byteCode l
| (SJump n :: l) -> "7 " + string n + " " + byteCode l
Run Code Online (Sandbox Code Playgroud)
这可能无法编译,因为我没有给出我的整个程序.
This returns: "0 10 0 2 1 0 32 0 4 0 5 1 3 1 "
I expect: "0 10 0 2 1 0 32 0 4 0 5 1 3 1"
Run Code Online (Sandbox Code Playgroud)
像这样的情况通常表明字符串以过于幼稚的方式连接在一起.首先考虑收集结果的所有单个组件,然后调用预定义String.concat函数:
let byteCode (l : sInstr list) : string =
let rec byteCode' l =
match l with
| [] -> []
| (SC n :: l) -> "0" :: string n :: byteCode' l
| (SAdd :: l) -> "1" :: byteCode' l
| (SSub :: l) -> "2" :: byteCode' l
| (SMul :: l) -> "3" :: byteCode' l
| (SNeg :: l) -> "4" :: byteCode' l
| (SLess :: l) -> "5" :: byteCode' l
| (SIfze n :: l) -> "6" :: string n :: byteCode' l
| (SJump n :: l) -> "7" :: string n :: byteCode' l
l |> byteCode' |> String.concat " "
Run Code Online (Sandbox Code Playgroud)
String.concat已经只在各个部分之间添加了分隔符字符串.
这也更加清晰,因为它将特定分隔符字符串的实现细节保留在核心逻辑之外,并使其更容易更换 - 想象一下,只需将其更改为函数中的两个空格即可.
或者,您可以只使用现有函数,并在最终生成的字符串上调用.Trim()(或.TrimEnd())方法来删除(尾随)空格.