62251

Question:
Im new to Haskell!! I wrote this code:
import Data.List
inputIndex :: [String] -> [String] -> Bool
inputIndex listx input = and [x `elem` listx |x <- input]
inputIndex = if inputIndex == true
then putStrLn ("ok")
It works fine without the if
statement but when I put the if
statement the following error is shown:
Syntax error in expression (unexpected `}', possibly due to bad layout)
</blockquote>What am I doing wrong here?
Thanks
Answer1:A couple of things are wrong here:
<ul><li>You will <em>need</em> an else clause.</li> <li>True
must be capitalized.</li>
<li>inputIndex
must always take two arguments (right now it does not, in the last case).</li>
</ul>I guess you want something like this...
inputIndex :: [String] -> [String] -> IO ()
inputIndex listx input = if inputIndex' listx input
then putStrLn ("ok")
else putStrLn ("not ok")
where
inputIndex' :: [String] -> [String] -> Bool
inputIndex' listx input = and [x `elem` listx |x <- input]
(Here I defined a new function with a near-identical name, by appending a prime/apostrophe. By defining it in the where
clause, it is only visible to the outer inputIndex
function. You can call this a helper-function, if you will. I could also have chosen a completely different name, but I'm uncreative.)
You could also condense this to the following (which is also more general):
allPresent :: (Eq t) => [t] -> [t] -> IO ()
allPresent xs ys = putStrLn (if and [y `elem` xs | y <- ys] then "ok" else "not ok")
Answer2: