[英]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. 这不起作用...我收到错误FS0001:类型'string'与最后一行的类型'seq'不兼容。 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
. 问题是
printf "%s " head
表示head
必须是一个string
,但实际上您希望它是一个char
,因此您将看到Parse
推断出了类型string list -> 'a option
。 Therefore, F# expects Seq.toList
to be applied to a string seq
, not a string
. 因此,F#希望将
Seq.toList
应用于string seq
,而不是string
。
The simple fix is to change the line doing the printing to printf "%c " head
. 简单的解决方法是将打印行更改为
printf "%c " head
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.