/ / Haskell Mod und Int zu Numeral - haskell, integer, int

Haskell Mod und Int to Numeral - Haskell, Ganzzahl, int

Ich mache eine meiner Hausaufgaben, um eine Ganzzahl kleiner als 5000 in eine römische Zahl umzuwandeln.

Hier ist mein Code für 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

Entschuldigung für den ursprünglichen Beitrag, bei dem ich die Fehlermeldung nicht deutlich angezeigt habe. Und hier ist die Fehlermeldung:

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.

Antworten:

1 für die Antwort № 1

Dies ist nicht wirklich eine Antwort auf Ihre Frage. Es ist eine Antwort auf Ihre Aufgabe. Als solches sollte es nicht als richtig gewertet werden, aber ich dachte, jemand würde es gerne sehen.

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 = []

Inspiriert von diese Implementierung