如何使用减号而不是波形符将负整数转换为 SML 中的字符串?

发布于 2024-12-16 15:05:05 字数 208 浏览 2 评论 0原文

标准 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-12-23 15:05:05

简而言之,不可以。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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文