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
Hello,
[...] "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?
Because "<-" is no operator but belongs to the syntax of the whole list expression.
[...]
Wolfgang
participants (2)
-
Cagdas Ozgenc -
Wolfgang Jeltsch