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
.
精彩评论