Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I put a boolean into a list and output it in Haskell?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading