开发者

This F# code is not working

开发者 https://www.devze.com 2022-12-13 21:43 出处:网络
This is not working... I geterror FS0001: The type \'string\' is not compatible with the type \'seq\' for the last line. Why?

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

精彩评论

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