[英]Specifying seq<int> as return type in F#
我想做一个接受字符串并返回数字序列的函数。 如果传递了空字符串,则应返回仅包含0的序列。 我尝试执行以下操作:
let mapToInt (s: string) :seq<int> =
if s.Length = 0 then
seq {0}
else
s.Split ' '
|> Seq.map int
但是,这给出了以下错误消息:
This expression should have type 'unit', but has type 'int'. Use 'ignore' to discard the result of the expression, or 'let' to bind the result to a name.
我的代码有什么问题?
您的序列表达式需要使用yield
来产生一个值:
if s.Length = 0 then
seq { yield 0 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.