简体   繁体   English

指定序列 <int> 作为F#中的返回类型

[英]Specifying seq<int> as return type in F#

I want to make a function that takes a string and returns a sequence of numbers. 我想做一个接受字符串并返回数字序列的函数。 It should return a sequence containing only 0 if it passed a empty string. 如果传递了空字符串,则应返回仅包含0的序列。 I tried doing the following: 我尝试执行以下操作:

let mapToInt (s: string) :seq<int> = 
    if s.Length = 0 then
        seq {0} 
    else
        s.Split ' ' 
        |> Seq.map int

This however gives the following error message: 但是,这给出了以下错误消息:

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.

What's wrong with my code? 我的代码有什么问题?

Your sequence expression needs to use yield to yield a value: 您的序列表达式需要使用yield来产生一个值:

if s.Length = 0 then
    seq { yield 0 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM