Parse error in pattern: putStrLn Possibly caused by a missing 'do'? [duplicate] - haskell

I just wrote my first Haskell program, but there is an error that I cannot understand. I think it is right because I just wrote it like the example from a book. Could anyone help me please?
main = do
putStrLn "Hello, what's your name?"
name <- getLine
putStrLn ("Hey" ++ name ++ ", nice to meet you!")
The error message is:
parse error on input 'putStrLn'
It is strange.

Though it's impossible to tell from your posted code because SO converts tabs to spaces at least some of the time, the problem is likely that you input a literal tab character before putStrLn instead of four spaces as you did for the other two lines in your do block, or vice versa.
All of the statements in a do block must start with the exact same whitespace, and not just appear to line up visually. If you're using a text editor that can display literal tabs in a special way, set it up to do so; it will save you some headaches.

Related

Haskell the function 'main' is not defined?

Here is my basic program, but it states the function 'main' is not defined in module 'Main' how can I fix this?
here is my program
main = do
-- variable
a <- getLine
putStrLn a
Your code is missing indentation, Haskell uses indentation to figure out where a block ends.
main = do
a <- getLine
putStrLn a
Above is the proper indented form of your code; you should probably read the article here which explains it far better than I.
This error message means simply that the compiler didn't find a definition of your function main.
To run your compiled program, rather than interact with it in ghci (which I'd recommend you do as a beginner), you need main::IO ().
If you don't give your module a name, it automagically does the equivalent of inserting module Main where at the top of your file.
I can't think of any way to produce this error other than to
accidentally comment out main with -- or {- other comment syntax -}
spell the word main incorrectly
accidentally compile an empty file.
(
Although your question appears to show incorrect indentation, that's because this site does not treat tabs as 8 characters wide. I suspect you indented the main by four spaces to get it to format as code in your question. In any case the compiler didn't give an error message consistent with an indentation error.
I'd like to recommend you use spaces rather than tabs for indentation, as it's unfailingly irritating to have to debug the whitespace of your program.
Most editors can be configured to turn a tab key press into an appropriate number of spaces, giving you the same line-it-up functionality with none of the character count discrepancies.
)

Haskell Parsec strange issue with multiple expression occurrences

here is the code which to my mind shouldn't cause any issue but for some reason does?
program = expr8
<|> seqOfStmt
seqOfStmt =
do list <- (sepBy1 expr8 whiteSpace)
return $ if length list == 1 then head list else Seq list
I get 3 errors all in respect to 'list' not being in scope?
It's probably blatantly obvious what is going wrong but I can't figure out why
If there are any alternatives to this I would greatly like to hear them !
Thanks in advance,
Seán
Your final line uses a tab character for indentation, while the other lines use spaces only.
You have tabs set to four spaces in your editor, but ghc uses eight character tab stops (just as terminals do).
Therefore your return line is parsed as a continuation of the previous line, and list is not yet in scope.
One easy way to fix this is to refrain from using tabs: use spaces only.
Once you've fixed that, your next error will probably be a type error: head list and Seq list have different types (unless perhaps you have redefined head for some reason). It's not clear why you want to treat the list differently if it contains only a single element.

How to make Haskell or ghci able to show Chinese characters and run Chinese characters named scripts?

I want to make a Haskell script to read files in my /home folder. However there are many files named with Chinese characters, and Haskell and Ghci cannot manage it. It seems Haskell and Ghci aren't good at displaying UTF-8 characters.
Here is what I encountered:
Prelude> "让Haskell或者Ghci能正确显示汉字并且读取汉字命名的文档"
"\35753Haskell\25110\32773Ghci\33021\27491\30830\26174\31034\27721\23383\24182\19988\35835\21462\27721\23383\21629\21517\30340\25991\26723"
Prelude> putStrLn "\35753Haskell\25110\32773Ghci\33021\27491\30830\26174\31034\27721\23383\24182\19988\35835\21462\27721\23383\21629\21517\30340\25991\26723"
让Haskell或者Ghci能正确显示汉字并且读取汉字命名的文档
GHC handles unicode just fine. These are the things you should know about it:
It uses your system encoding for converting from byte to characters and back when reading from or writing to the console. Since it did the conversion from bytes to characters properly in your example, I'd say your system encoding is set properly.
The show function on String has a limited output character set. The show function is used by GHCI to print the result of evaluating an expression, and by the print function to convert the value passed in to a String representation.
The putStr and putStrLn functions are for actually writing a String to the console exactly as it was provided to them.
Thanks to Carl, i used putStrLn as a wrapper around my fuction:
ghci> let removeNonUppercase st = [c | c <- st, c `elem` ['А'..'Я']]
ghci> putStrLn (removeNonUppercase "Ха-ха-ха! А-ха-ха!")
ХА
Everything works fine!

GHCi and compiled code seem to behave differently

I have a very strange problem. The following code gives different results when compiled as compared to running in ghci,
main = do
putStr "Please enter your name: "
name <- getLine
putStr ("Hello, " ++ name ++ ", how are you?")
When run it in ghci it does as one would expect,
Please enter your name: dglmoore
Hello, dglmoore, how are you?
However, when I compile the code to an executable it requires that I provide the input before any output is generated so I end up with this,
dglmoore
Please enter your name: Hello, dglmoore, how are you?
I've seen a similar problem before, but I cannot seem to find it again.
I am using ghc version 7.4.1 from the Haskell Platform version 2012.2.0.0.
Anyone have any idea why they give different results and how I can get both versions to do the "correct" thing?
It's a buffering issue. Usually IO is line buffered (i.e. the output doesn't actually show up on the screen until you print a new line or exceed the buffer size) unless you explicitly flush the buffer. In ghci it isn't, so the issue doesn't show up.
You can use hFlush stdout to flush stdout, causing the output to be printed to the screen, before you call getLine.
Alternatively you can use hSetBuffering NoBuffering to disable buffering altogether, removing the need for hFlush. That could have a negative impact on IO performance though.

Haskell indentation error [duplicate]

This question already has an answer here:
Why shouldn't I mix tabs and spaces?
(1 answer)
Closed 6 years ago.
Why is the following code not correct ? i get this error: Last generator in do {...} must be an expression?
main = do putStrLn "What is 2 + 2?"
x <- readLn
if x == 4
then putStrLn "You're right!"
else putStrLn "You're wrong!"
You are mixing tabs and spaces: the second and fifth line contain tabs, while the third and fourth don't.
The Haskell compiler probably expands tabs to a different number of spaces than your editor and what looks correctly indented in the editor looks messed up to the compiler.
Best avoid mixing tabs and spaces and only use one of them for indentation.
You had tabulations instead of spaces. After you have pasted the code into stackoverflow there are only spaces and everything is working.

Resources