list comprehension

Rijk-Jan van Haaften rjchaaft@cs.uu.nl
Fri, 19 Oct 2001 13:07:09 +0200


--=====================_29017334==_.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed

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".
--=====================_29017334==_.ALT
Content-Type: text/html; charset="us-ascii"

<html>
Hello,<br>
<br>
<blockquote type=cite cite><font face="arial" size=2>Hi, </font><br>
<font face="arial" size=2>I have a script here</font><br>
&nbsp;<br>
<font face="arial" size=2>interleave :: [Integer] -&gt; [Strings] -&gt;
[String]<br>
interleave (x:xs) (y:ys)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = words [a | a &lt;- (unwords
[(show x), (filter (/= 1) y), &quot;+&quot;])]&nbsp;&nbsp;
</font></blockquote><br>
Firstly, &quot;Strings&quot; is not a standard type. I suspect<br>
you mean &quot;String&quot; so the type signature is<br>
<font face="arial" size=2>interleave :: [Integer] -&gt; [String] -&gt;
[String]<br>
<br>
Now consider your sub-expression<br>
<blockquote type=cite cite>filter (/= 1) y</font></blockquote><br>
y is a string (as it is the first element of a list of strings)<br>
filter will use it as a list of characters though because<br>
String == [Char] in haskell and filter expects a list.<br>
<br>
1 is a number; the haskell type is Num a =&gt; a<br>
which means: 1 is of type a provided that it is an instance<br>
of class Num (that is: provided that it is a number)<br>
All usual number types are instance of class Num:<br>
Int, Integer, Double, Float<br>
<br>
Filter will compare 1 against each element of the character list,<br>
using /=.<br>
<br>
Let's say: y = &quot;abc&quot;.<br>
In list notation: y = ['a', 'b', 'c']<br>
<br>
now filter will attempt three comparisons<br>
1 /= 'a'<br>
1 /= 'b'<br>
1 /= 'c'<br>
However, the compiler detects that characters ('a', 'b' and 'c'
here)<br>
are not numbers, so this comparison is invalid.<br>
<br>
Conclusion:<br>
If you try to compare an expression which yields a number with<br>
some expression which doesn't yield a number, hugs will tell you<br>
that the second expression &quot;Char is not an instance of class
Num&quot; or<br>
&quot;instance of Num Char required&quot;<br>
which you should read: &quot;Char is not a number and it needs to 
be<br>
in order to be compared with a number&quot;.</html>

--=====================_29017334==_.ALT--