So I am new to Haskell and below I have attempted to program a function that takes a given value of e and a given list and determines whether that given value appears in the list given outputting True if the value given does appear and False if not.
inListm e [] = False
inListm e (x:xs)
| e == x = True || inListm e xs
| otherwise = False || inListm e xs
If
inListm 2 [0, 2, 1, 2]
is given, the output would be
True
However, I would like the final output to be in a list like this
[True]
I have attempted to do this by
inListd e [] = False : []
inListd e (x:xs)
| e == x = True : [] || inListd e xs
| otherwise = False :[] || inListd e xs
but all that gives me is an error so I would like to know how I could resolve this
CodePudding user response:
You are on the right track. The only thing necessary is to return a list if you have a result, so:
inListd :: Eq a => a -> [a] -> [Bool]
inListd e [] = [False]
inListd e (x:xs)
| e == x = [True]
| otherwise = inListd e xs
That being said, I does not seem to make much sense to wrap the result in a list.
CodePudding user response:
Consider reusing your existing function; reuse is a big part of what makes writing big programs possible. Like this:
inListd e xs = inListm e xs : []
-- OR
inListd e xs = [inListm e xs]