我是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来调用它。

关于f# - Seq.find返回'a-> int而不是int,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18129331/

10-12 12:45
查看更多