free variable

Cagdas Ozgenc co19@cornell.edu
Thu, 9 Aug 2001 11:26:51 +0300


Hi,

I still have confusing regarding the following case:

elemNum2 :: Int -> [Int] -> Int
elemNum2 elem list = sum [ 1 | findElem <- list, elem == findElem ]

the function I wrote finds the number of occurences of an element in a list.
If I change it to the following:

elemNum2 elem list = sum [ 1 | elem <- list ]

Now it simply gives the length of the list. OK I understand that "elem" in
the list comprehension defines a new variable on the fly. What I do not
understand is in the first case:

"findElem <- list" findElem is a new variable, but "list" is not.
"elem==findElem" here for some reason "elem" is not a new variable.

Why does the rule only apply for "<-" operation, but not "==" for example?

Thanks