Hi, I have a script here interleave :: [Integer] -> [Strings] -> [String] interleave (x:xs) (y:ys) = words [a | a <- (unwords [(show x), (filter (/= 1) y), "+"])] and the error message i receive is (Instance of Num Char required for definition of interleave) and I don't understand what this is asking of me. Please help if you are able to!!! Thanks.....Steph
"Stephanie" == Stephanie Randles <srandles@bigpond.net.au> writes:
Stephanie> Hi, I have a script here Stephanie> interleave :: [Integer] -> [Strings] -> [String] Stephanie> interleave (x:xs) (y:ys) = words [a | a <- (unwords Stephanie> [(show x), (filter (/= 1) y), "+"])] Stephanie> and the error message i receive is Stephanie> (Instance of Num Char required for definition of Stephanie> interleave) the difficult error message is due to the flexibility of the type class system in Haskell. The variable y is of type String, i.e., a list of Char. You try to compare a number (/= 1) with a character (an element of y taken by filter). The number constant 1 imposes the type class Num (for numbers) via the comparison to this character, but you have not made Char an instance of class Num (define +, -, * on Char). Probably that's not what you want; the error message does not address the logical source of error but the point where the constraints mismatched. Good luck -- Christoph Herrmann
Hello,
Hi, I have a script here
interleave :: [Integer] -> [Strings] -> [String] interleave (x:xs) (y:ys) = words [a | a <- (unwords [(show x), (filter (/= 1) y), "+"])]
Firstly, "Strings" is not a standard type. I suspect you mean "String" so the type signature is interleave :: [Integer] -> [String] -> [String] Now consider your sub-expression
filter (/= 1) y
y is a string (as it is the first element of a list of strings) filter will use it as a list of characters though because String == [Char] in haskell and filter expects a list. 1 is a number; the haskell type is Num a => a which means: 1 is of type a provided that it is an instance of class Num (that is: provided that it is a number) All usual number types are instance of class Num: Int, Integer, Double, Float Filter will compare 1 against each element of the character list, using /=. Let's say: y = "abc". In list notation: y = ['a', 'b', 'c'] now filter will attempt three comparisons 1 /= 'a' 1 /= 'b' 1 /= 'c' However, the compiler detects that characters ('a', 'b' and 'c' here) are not numbers, so this comparison is invalid. Conclusion: If you try to compare an expression which yields a number with some expression which doesn't yield a number, hugs will tell you that the second expression "Char is not an instance of class Num" or "instance of Num Char required" which you should read: "Char is not a number and it needs to be in order to be compared with a number".
participants (3)
-
Ch. A. Herrmann -
Rijk-Jan van Haaften -
Stephanie Randles