sml中将字符串转换为实数
我需要将字符串类型转换为 sml 中的实际类型,而不使用 sml 中的 Real.fromString 函数。
例如:输入“12.3”->输出12.3:real
I need to convert a string type to real type in sml without using the
function Real.fromString in sml.
For example: input "12.3"->output 12.3:real
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这没有意义。对于初学者来说,Real 模块本身并不是一个外部库,而是每个 SML 实现所必需的基础库 的一部分应该提供(请参阅此链接)。
如果您不想使用基础库的任何部分,那么我建议您根本不要做任何事情,因为它会让您几乎什么也没有。
另一方面,如果您想自己实现这个乐趣,那么您将必须解析字符串并一路生成一个实数。基础库使用真正的扫描仪来完成此操作,并且
StringCvt.scanString
。真实扫描仪的 MLton 实现大约有 245 行,这还不包括 Char 模块等辅助功能。如果这是您想做的,那么看看他们是如何实现的。否则,只需使用基础库中的函数即可,这就是它们的用途。
This doesn't make sense. For starters the Real module is not a external library as such, but a required part of the basis library that every SML implementation ought to supply (see this link).
If you don't want to use any part of the basis library, then I would advise you not to do anything at all as it will leave you with almost nothing.
On the other hand if you want to implement this yourself for fun then you will have to parse the string and produce a real along the way. The basis library does this using a real scanner and
StringCvt.scanString
. The MLton implementation of the real scanner is about 245 lines not counting the auxiliary functions that help it on the way such as the Char module.If this is what you want to do, then take a look at how they implement it. Else, just use the functions from the basis library, that is what they are there for.
尝试
Real.fromString
,它的类型为string ->实物期权
:try
Real.fromString
, which has typestring -> real option
: