简体   繁体   中英

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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