62251

syntax in expression - Haskell

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:

<blockquote>

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:

<ol><li>It's "True", not "true".</li> <li>Your second inputIndex implementation is not compatible with the first one. All your pattern cases for a function must have the same signature ([String] -> [String] -> Bool)</li> <li>The error you show here is not generated by this code, because there is no '}' here.</li> <li>putStrLn has signature String -> IO() while your inputIndex looks like it's supposed to be pure - just return the value and print it somewhere else.</li> </ol>

Recommend

  • What is wrong in my MVC implementation?
  • android-support-v7-appcompat has same attrs as actionbarsherlock library
  • command line of process by name
  • SQL Server re-calculate or not?
  • Does the MySQL IN clause execute the subquery multiple times?
  • Unique Permutations - with exceptions
  • How to create two column output from a single column
  • .NET video play library which allows to change the playback rate?
  • ZipList with Scalaz
  • Connecting bluetooth device to windows phone 8 application
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can I get the choice “H2” back in the H2 consol?
  • Which open source license has no forking [closed]
  • How to 'create temp table as select' in Slick?
  • CXF JAXB JAXBEncoderDecoder unmarshalling error : unexpected element when having qualified elements
  • Unexpected token ILLEGAL while running node.js mocha test
  • NUnit 3.0 TestCase const custom object arguments
  • Plotting line graph with factors in R
  • Question about instantiating object
  • Approximate Order-Preserving Huffman Code
  • MySQL Order by column = x, column asc?
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Email verification using google app script and google forms
  • Java: can you cast Class into a specific interface?
  • FFmpeg Conversion Error
  • Sony Xperia Z Tablet not found by adb
  • AES padding and writing the ciphertext to a disk file
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Weird JavaScript statement, what does it mean?
  • How can I estimate amount of memory left with calling System.gc()?
  • Calling of Constructors in a Java
  • Is there a mandatory requirement to switch app.yaml?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass