0

Hi all,

i need a little help for haskell.

i try to make a program which makes the romans numbers in a good form, so for example if u put in [I,I,I,I] it change it to [I,V] but something always wrong with the patterns :( what did i wrong?

data RomanNumeral = I | V | X | L | C | D | M
  deriving (Show, Eq, Ord, Data, Typeable)

type RomanNumber = [RomanNumeral]

compact :: [RomanNumeral] -> [RomanNumeral]
compact [] = []
compact (x:y:a:s:d:xs)
        | (x:y:a:s:d) == [D,C,C,C,C] = [C,M] ++ compact xs
        | (x:y:a:s) == [C,C,C,C] = [C,D] ++ compact (d:xs)
        | (x:y:a:s:d) == [L,X,X,X,X] = [X,C] ++ compact xs
        | (x:y:a:s) == [X,X,X,X] = [X,L] ++ compact (d:xs)
        | (x:y:a:s:d) == [V,I,I,I,I] = [I,X] ++ compact xs
        | (x:y:a:s) == [I,I,I,I] = [I,V] ++ compact (d:xs)
        | otherwise = [x] ++ compact (y:a:s:d:xs)

if u know what did i wrong pls help asap

thx
W

3
Contributors
2
Replies
7
Views
5 Years
Discussion Span
Last Post by sepp2k
0

(x:y:a:s:d) (and the other expressions like it) will cause a type error because the right operand to : needs to be a list and here it's not. The proper way to achieve what you're trying do would be to use pattern matching like this:

compact (D:C:C:C:C:xs) = [C,M] ++ compact xs
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.