我是F#的新手。我正在尝试创建一个F#程序,将数字转换为对应的罗马数字。
type RomanDigit = I | IV | V | IX
let rec romanNumeral number =
let values = [ 9; 5; 4; 1 ]
let toRomanDigit x =
match x with
| 9 -> IX
| 5 -> V
| 4 -> IV
| 1 -> I
let capture x =
values
|> Seq.find ( fun x -> number >= x )
match number with
| 0 -> []
| int -> Seq.toList ( Seq.concat [ [ toRomanDigit capture ]; romanNumeral ( number - capture ) ] )
我的问题是捕获的类型是"a->int",但考虑到Seq.find将返回一个int,我希望它的类型是int。特别是,我随后对捕获的调用抛出了一个错误,特别是在:
| int -> Seq.toList ( Seq.concat [ [ toRomanDigit capture ]; romanNumeral ( number - capture ) ] )
我做错了什么?
您的
let capture x =
values
|> Seq.find (fun x -> number >= x)
会被解读为这样的东西:
设capture
为函数,该函数给定一个输入x
,忽略该输入并返回values |> Seq.find (fun x -> number >= x)
。所以,也许你想要
let capture = values |> Seq.find (fun x -> number >= x)
或
let capture values = values |> Seq.find (fun x -> number >= x)
在后一种情况下,它是一个适当的函数,您可以用capture values
而不仅仅是capture
来调用它。
capture
应该是值而不是函数吗?如果是,请删除参数:
let capture =
values
|> Seq.find ( fun x -> number >= x )