-2

我正在做一项作业,将小于 5000 的整数转换为罗马数字。

这是我的 intToRoman 代码

mrepeat :: Integer -> String -> String
mrepeat numRepeat strRepeat
 | numRepeat == 0 = ""
 | otherwise = strRepeat ++ mrepeat (numRepeat - 1) strRepeat

romanLetter :: [String]
romanValue :: [Int]
romanLetter = ["I", "V", "X", "L", "C", "D", "M"]
romanValue = [1, 5, 10, 50, 100, 500, 1000]
convertIter :: Int -> Integer -> String
convertIter count number
 | count == -1 = ""
 | mod count 2 == 0 && q == 4 = (romanLetter !! count) ++ (romanLetter !! (count + 1)) ++ (convertIter (count - 1) r)
 | mod count 2 == 1 && (mod (number + (romanValue !! (count - 1))) romanValue !! count) == 2 = (romanLetter !! (count - 1)) ++ (romanLetter !! (count + 1)) ++ (convertIter (count - 1) r)
 | otherwise = (mrepeat q (romanLetter !! (count - 1))) ++ (convertIter !! (count - 1) r)
 where (q, r) = divMod number romanValue !! count

intToRoman :: Integer -> String
intToRoman number = convertIter 6 number

很抱歉我没有清楚地显示错误消息的原始帖子。这是错误消息:

test.hs:14:25:
    No instance for (Integral [a0])
      arising from a use of `mod'
    Possible fix: add an instance declaration for (Integral [a0])
    In the first argument of `(!!)', namely
      `mod (number + (romanValue !! (count - 1))) romanValue'
    In the first argument of `(==)', namely
      `(mod (number + (romanValue !! (count - 1))) romanValue !! count)'
    In the second argument of `(&&)', namely
      `(mod (number + (romanValue !! (count - 1))) romanValue !! count)
     ==
       2'

test.hs:14:30:
    Couldn't match expected type `[a0]' with actual type `Integer'
    In the first argument of `(+)', namely `number'
    In the first argument of `mod', namely
      `(number + (romanValue !! (count - 1)))'
    In the first argument of `(!!)', namely
      `mod (number + (romanValue !! (count - 1))) romanValue'

test.hs:14:37:
    No instance for (Num [a0])
      arising from a use of `+'
    Possible fix: add an instance declaration for (Num [a0])
    In the first argument of `mod', namely
      `(number + (romanValue !! (count - 1)))'
    In the first argument of `(!!)', namely
      `mod (number + (romanValue !! (count - 1))) romanValue'
    In the first argument of `(==)', namely
      `(mod (number + (romanValue !! (count - 1))) romanValue !! count)'

test.hs:16:17:
    Couldn't match expected type `[(t0, t1)]'
                with actual type `(a0, a0)'
    In the return type of a call of `divMod'
    In the first argument of `(!!)', namely `divMod number romanValue'
    In the expression: divMod number romanValue !! count
Failed, modules loaded: none.
4

1 回答 1

1

这不是问题的真正答案。这是对你任务的回答。因此,它不应该被认为是正确的,但我认为有人愿意看到它。

intToRoman :: Integer -> String
intToRoman n
  | n >= 1000 = 'M'       : intToRoman (n - 1000)
  | n >= 900  = 'C' : 'M' : intToRoman (n - 900)
  | n >= 500  = 'D'       : intToRoman (n - 500)
  | n >= 400  = 'C' : 'D' : intToRoman (n - 400)
  | n >= 100  = 'C'       : intToRoman (n - 100)
  | n >= 90   = 'X' : 'C' : intToRoman (n - 90)
  | n >= 50   = 'L'       : intToRoman (n - 50)
  | n >= 40   = 'X' : 'L' : intToRoman (n - 40)
  | n >= 10   = 'X'       : intToRoman (n - 10)
  | n >= 9    = 'I' : 'X' : intToRoman (n - 9)
  | n >= 5    = 'V'       : intToRoman (n - 5)
  | n >= 4    = 'I' : 'V' : intToRoman (n - 4)
  | n >= 1    = 'I'       : intToRoman (n - 1)
  | otherwise = []

受此实施的启发

于 2013-10-25T14:51:24.187 回答