When I run this code:
test1 :: Int -> String
test1 x = do
if x == 1
then "Hello"
I get the following error:
test-if.hs:4:21: error:
parse error (possibly incorrect indentation or mismatched brackets)
I am not sure why this is as I am not using any brackets and I am using 4 spaces as my tabs. Adding brackets doesn't seem to help. What could be the issue?
Thanks
Your if needs an else (what do you want the value to be when x isn't 1?).
Furthermore do notation is used when working with monads and doesn't make sense in this function.
Related
The code below is for a calculator I have written. I am trying to handle parse error and token error but I keep receiving the same error on ∖s -> tokens s How do I fix this? Why do I keep getting this error?
{- main -}
main = do cs <- getContents
putStr $ unlines $ map show $
map (∖s -> tokens s >= parse >= eval) $ lines cs
The problem here is a subtle one. The correct syntax is \s -> tokens s >= parse >= eval, using a backslash: \ U+005C REVERSE SOLIDUS. However, instead your code is ∖s -> tokens s >= parse >= eval. This is subtly different: instead of using a backslash as expected, it instead uses ∖ U+2216 SET MINUS. Simply use the correct character, by replacing ∖ with \, and it should parse correctly.
I am trying to generate a list which will contain a list of solutions for the problem I am trying to solve. I am doing this using list comprehension, generating all possible combinations and then using a predicate to filter so that only solutions are included. Here is my code.
solveRow :: Row -> [Row]
solveRow (Row target rowCells) =
[(cellsToRow target cls) | (cls <- (cellPermutations rowCells)), (result cls)==target]
cellPermutations returns [[Cell]] and therefore cls should be of type [Cell]. When I try to compile I get an error.
I thought <- passed each element of right hand side (in this case [Cell]) to the left hand side variable. Why am I getting a parse error for <-?
Don’t put brackets around cls <- (cellPermutations rowCells) — that’s a syntax error. You don’t need brackets around cellsToRow target cls either, although doing so isn’t an error. (And strictly speaking, you don’t need brackets around result cls either, but I personally think that those brackets make it more readable.) So your example should be:
solveRow :: Row -> [Row]
solveRow (Row target rowCells) =
[cellsToRow target cls | cls <- (cellPermutations rowCells), (result cls)==target]
I am getting this compile error:
Files.hs:47:17: parse error on input ‘->’
Failed, modules loaded: none.
In the following section of code:
main :: IO ()
main = do
args <- getArgs
let f = case args of
("W":_) -> eoltoW
-- ^ here's 47:17
("U":_) -> eoltoU
_ -> fail "2 - 3 arguments required"
case args of
[_,i,o] -> editWith f i o
[_,i] -> catWith f i
[_] -> fail "2 - 3 arguments required"
While I understand the logic could use some tidying up, I do not see where I am going wrong with case syntax. I figure it might be some weird interaction with do and let, but I can't find any clue as to how to correct it.
Note, I have ensured that I only use spaces for indentation
Edit:
It seems that adding a single space further of indentation (as below) is sufficient to prevent the error, but I am unclear as to why.
main = do
args <- getArgs
let f = case args of
("W":_) -> eoltoW
-- ^ here's 47:17
("U":_) -> eoltoU
_ -> fail "2 - 3 arguments required"
This is described in 2.7 and 10.3. Basically, the rule for let … in a do block* is that all bindings have to be indented the same way:
let a = …
b = …
c = …
Furthermore, the "…" have to be more indented than the layout list. For example, the following is a syntax error:
let a =
10
in a
In order to create a new layout-list, we need to indent it further (see note 1 in section 10.3), which is why
let a =
10
in a
is completely fine. The same holds for your case. All of the cases have to be further indented than f due to the off-side rule.
* that rule actually holds** for more, i.e. for let, where, do and of.
** well, as long as you don't introduce additional braces
I was supposed to write a little function in Haskell, which should erase elements, which are twice in the list. Unfortunately, Haskell complains " parse error on input `|' ". Could anyone help me with that?
makeSets=mSet[]s
where
mSet stack []=stack
mSet stack (x:xs)
|contains stack x=mSetstack xs
| otherwise =mSet (x:stack) xs
where
contains [] thing=False
contains (x:xs)thing
| x==thing=True
|otherwise=contains xs thing
You are mixing tabs and spaces, which is no good when indentation is significant. Use either all spaces (strongly recommended), or all tabs.
I am trying to build a function that reacts differently depending on whether the parameter is an empty list or a list containing something. My code:
validateCypher :: [Char] -> [Char]
validateCypher [] = cypherB
validateCyper n:[] = ['O']
Which however returns
Syntax error in declaration (unexpected symbol ":")
Ideas? :)
You need to put (..) around n:[]. But note that (n:[]) is the same as [n].