2

I'm new with F#. I'm trying to create an F# program transforming a number to its roman numeral counterpart.

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 ) ] )

My problem here is that capture has the type 'a -> int, but I expect it to have the type int, considering Seq.find will return an int. Particularly, my subsequent calls to capture throws an error particularly in:

| int -> Seq.toList ( Seq.concat [ [ toRomanDigit capture ]; romanNumeral ( number - capture ) ] )

What am I doing wrong?

4

2 回答 2

4

应该capture是一个值而不是一个函数?如果是这样,请删除参数:

let capture =
    values
    |> Seq.find ( fun x -> number >= x )
于 2013-08-08T15:04:37.377 回答
4

您的

let capture x =
    values
    |> Seq.find (fun x -> number >= x)

会被读成这样:

letcapture是一个给定输入的函数,它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而不是 just 来调用它capture

于 2013-08-08T15:22:55.973 回答