Types Can';无法在F#函数中获取要匹配的类型
我正在尝试将字符串转换为罗马数字类型列表。但我不明白seq.map是如何工作的 更改charromdig中的类型Types Can';无法在F#函数中获取要匹配的类型,types,f#,matching,Types,F#,Matching,我正在尝试将字符串转换为罗马数字类型列表。但我不明白seq.map是如何工作的 更改charromdig中的类型 let charRomDig c:RomDig = match c with | "I" -> I | "V" -> V | "X" -> X | _ -> I let toUpper (s:string) = s.ToUpper() let strToRom (str:s
let charRomDig c:RomDig =
match c with
| "I" -> I
| "V" -> V
| "X" -> X
| _ -> I
let toUpper (s:string) =
s.ToUpper()
let strToRom (str:string): RomNum =
let res = toUpper str
let xTimes2 =
res
|> Seq.map(charRomDig)
romL
let res = strToRom "XI"
我希望它能够成功地显示转换后的罗马数字列表
但我得到了以下信息:
'char -> 'a'
but given a
'string -> RomDig'
The type 'char' does not match the type 'string'
听起来您的Seq.map需要一个char来进行RomDig,但却收到了一个函数,该函数接受一个字符串来进行RomDig。这听起来不错,因为它看起来像是你的地图将字符串映射到RomDig。在将来,请确保包含类型定义,因为这会使您更容易获得帮助。在charRomDig这里,我注意到你的匹配是“I”“V”和“X”,而不是“I”“V”和“X”。当把字符串看作单个字符的SEQ时,匹配也需要是字符。
type RomDig = I | V | X
type RomNum = RomDig seq //equivalent to seq<RomDig>
let charRomDig c:RomDig = match c with | 'I' -> I | 'V' -> V | 'X' -> X | _ -> I
let strToRom (str:string): RomNum =
let xTimes2 = str |> Seq.map(charRomDig)
xTimes2
let XI = strToRom "XI"
类型RomDig=I | V | X
类型RomNum=RomDig seq//等同于seq
让charRomDig c:RomDig=将c与|‘I’->I |‘V’->V |‘X’->X |->I匹配
let strToRom(str:string):RomNum=
设xTimes2=str |>Seq.map(charRomDig)
X时刻2
让席=“席”