在 SML 中将字符串转换为实数
我想将实数的字符串表示形式转换为实数类型。 我知道我可以执行 Real.fromString("5.5")
但它不会返回实数类型,而是返回实数选项类型,我无法将其与任何其他实数相乘或相加。
I want to convert string representation of real number to real type.
I know that I can do Real.fromString("5.5")
but it doesn't return real type but real option type which I can't multiply or add with any other real.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过模式匹配或使用 Option 结构中的函数之一从选项中提取值。例如:
Extract the value from the option by pattern matching or using one of the functions in the Option structure. For example:
为了补充 Michael J. Barber 的答案,选项类型是一种代数数据类型,要么是某些东西,要么是无。
通常,在机器学习中,我们通常使用模式匹配来解构代数数据类型:
您可以像 Michael J. Barber 建议的那样使用
getOpt
(您实际上不需要Option.
,因为>getOpt
是在顶级环境中),这是上面的简化版本。或者,如果您确定它将是 SOME,则可以使用
valOf
(如果为 NONE,则会出错):或者您可以在
val 中对其进行模式匹配
(因为val
也是一种模式匹配,尽管只有一个分支):To add to Michael J. Barber's answer, the option type is an algebraic datatype which is either SOME something, or NONE.
Usually, in ML we usually deconstruct algebraic datatypes with pattern matching:
You could use
getOpt
like Michael J. Barber suggested (you don't actually need theOption.
sincegetOpt
is in the top-level environment), which is a simplified version of the above.Or, if you are sure that it is going to be a SOME, you could use
valOf
(which will error if it is NONE):or you could pattern-match it away in a
val
(sinceval
is also a pattern match, albeit with only one branch):