For my first lecture at haskell, we asked a number of problems. One of them is to return True when n is present in the list or False otherwise. I managed to get what, in my opinion, halfway, but I get different compilation errors and get quite tired because I can even understand what they mean.
So far I have done the following
// No problem with this function
matches :: Int -> [Int] -> [Int] // This function is to return the matches
matches x y = [a | a <-y, a==x] // ie. main> 1 [1,3,5,7,1,4] outputs [1,1]
// Here am stuck
myelem :: Int -> [Int] -> Bool
myelem x [] = False
myelem x (y:ys)
| x == y = y : x myelem ys // Am not sure about recursion as
// we have not yet covered
Obviously this is for the class, so please do not post the answer. But maybe a few examples that will help me justify how Haskell works and how to approach the problem. Any pointer would be widely appreciated.
Decision
matches :: Int -> [Int] -> [Int]
matches x y = [a | a <-y, a==x]
myelem :: Int -> [Int] -> Bool
myelem x [] = False
myelem x (y:ys)
| x == y = True
| otherwise = myelem x (ys)
Greetings guys
source
share