如何使用减号而不是波形符将负整数转换为 SML 中的字符串?
标准 SML 库函数 Int.toString 在负数前添加 ~
前缀,而不是 -
。有没有库函数可以用-
代替,缺少写法
fun i2s i =
if i < 0 then "-" ^ Int.toString (~i) else Int.toString i
The standard SML library function Int.toString prefixes negative numbers with ~
instead of -
. Is there a library function to use -
instead, short of writing
fun i2s i =
if i < 0 then "-" ^ Int.toString (~i) else Int.toString i
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,不可以。SML
被设计为使用
~
表示一元减号,以避免与-
(二进制减号)混淆。当每个操作员仅用于一个目的且 SML 用户必须接受这一点时,这是一个明智的决定。尽管读取以
~
开头的整数的字符串表示形式很奇怪,但没有库函数可以将其转换为正常约定中的字符串。顺便说一句,你的函数是一个很好的方法。In short, No.
SML is designed to use
~
for unary minus to avoid confusion with-
(binary minus). It's a sensible decision when you have each operator for only one purpose and SML users have to live with that.Although it's strange to read a string representation of an integer starting with
~
, there's no library function to convert it to a string in the normal convention. BTW, your function is a good way to do so.