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


Recommended Answers

All 2 Replies

It would help if you told us what the error was.

(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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.