簡體   English   中英

指定序列 <int> 作為F#中的返回類型

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM