[英]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.