Why is my variable not in scope? It seems everything is ok, but it does not work
myEven::[Int] -> [Int]
myEven [] = []
myEven (x:xs) = if x `mod` 2 == 0 then x:myEven xs
else myEven xs
I am testing now and it's working but without first line. Where is my mistake?
Another question, how to rewrite this function in Pattern matching
Why is my variable not in scope?
Perhaps you failed to load the file or perhaps something else, hard to say without seeing the error and your invocation. Either way, glad it's working.
Another question, how to rewrite this function in Pattern matching
You are already using pattern matching on the list. Do you want to pattern match on the Int
? You can't, for example, match on bits but I can see how that would be neat (highBits # 0b0) : xs = x : myEven xs
.
Instead you can use guards:
myEven ::[Int] -> [Int]
myEven [] = []
myEven (x:xs) | x `mod` 2 == 0 = x : myEven xs
| otherwise = myEven xs
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments