开发者

Haskell: Beginner syntax question

a very simple question from a Haskell learner. I am working through Yet Another Haskell Tutorial and I am stuck on a simple syntax practice. The code given below: When I copy and paste it (from pdf) and then adjust the indentation it works fine, but when I type it out into an editor (in my case Notepad++) then it throws the following error:

Guess.hs:8:9: parse error on input ´hSetBuffering´

I made sure that I did not mix tabs and whitespaces (4 whitespaces) and I did not find a typo in the book. I am sure it is a very simple mistake so thanks for any input.

Nebelhom

Here is the code:

module Main
    where

import IO
import Random

main = do
    hSetBuffering stdin LineBuffering
    num <- randomRIO (1::Int, 100)
    putStrLn "I'm thinking of a number between 1 and 100"
    doGuessing num

doGuessing num = do
    putStrLn "Enter your guess:"
    guess <- getLine
    let guessNum = read guess
    if guessNum < num
开发者_StackOverflow中文版        then do putStrLn "Too low!"
                doGuessing num
    else if read guess > num
        then do putStrLn "Too high!"
                doGuessing num
    else do putStrLn "You Win!"


There's no syntax error that I can see, or reproduce:

$ ghci A.hs
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling Main             ( A.hs, interpreted )
Ok, modules loaded: Main.

which means it is probably tabs. Did you insert a tab somewhere? And then use 4 spaces for indenting? Tabs are in general a bad idea in Haskell, as they lead to unintentional, and unintelligible, syntax errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜