开发者

This F# code is not working

This is not working... I get error FS0001: The type 'string' is not compatible with the type 'seq' for the last line. Why?

let rec Parse 开发者_运维百科(charlist) =
   match charlist with
   | head :: tail -> printf "%s " head
                     Parse tail
   | [] -> None

Parse (Seq.toList "this is a sentence.") |> ignore


The problem is that printf "%s " head means that head must be a string, but you actually want it to be a char, so you'll see that Parse has inferred type string list -> 'a option. Therefore, F# expects Seq.toList to be applied to a string seq, not a string.

The simple fix is to change the line doing the printing to printf "%c " head.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜