WinGHCi运行预设Haskell代码出现Parsing error问题求助
Hey there! Let's break down what's happening here and fix that parse error—it's not an issue with your Haskell Platform installation.
Why the Error Happens
WinGHCi's interactive mode parses input line-by-line by default. When you type area :: Shape -> Double and hit enter, GHCi tries to treat this line as a complete, executable expression. But since it's just a type signature followed by unfinished function definitions, GHCi gets confused and throws that parse error on the :: symbol.
Fixes to Try
You have a few simple ways to get your code working in WinGHCi:
Enable Multi-Line Mode
First run this command in GHCi to allow multi-line input handling::set +mThen paste or type your code line by line, and press enter twice when you're done. GHCi will recognize the entire block as a single definition.
Wrap Code in Block Delimiters
Surround your entire code block with:{and:}to tell GHCi to treat everything in between as one multi-line definition::{ data Shape = Circle Double | Rect Double Double area :: Shape -> Double area (Circle radius) = 3.1415 * radius * radius area (Rect width height) = width * height :}Load from a File (Recommended)
Save your code into a file named something likeShapes.hs, then in WinGHCi run::l Shapes.hsThis loads the entire file at once, which avoids interactive mode parsing quirks entirely and is the cleanest way to work with longer code snippets.
Once you use one of these methods, your code should compile smoothly. Test it out by calling area (Circle 5) or area (Rect 3 4) to see the results!
内容的提问来源于stack exchange,提问作者Legion Daeth

