1

I am getting a parse error on input '=' when trying to run the following code

module GiveNums
  where
import System.IO

main = do
  hSetBuffering stdin LineBuffering
  n <- giveNum
  sum = map (+) n
  putStrLn "The sum is " ++ show sum

giveNum = do
  hSetBuffering stdin LineBuffering
  putStrLn "Enter num"
  num <- getLine
  if read num == 0
    then return []
  else do
   rest <- giveNum
   return ((read num :: Int: rest)
3
  • 1
    In short: Because that isn't legal Haskell syntax. Longer: binding variables is let var = expr. Also: map (+) n won't give a single number but a list of functions, you probably want fold. Also: f x :: ty : xs won't parse right you need parens such as (f x :: ty) : xs). Commented Apr 25, 2017 at 2:52
  • Also sum is already taken as a variable name, it's the name of a function. Do a :t sum and a :i sum in ghci, or search it. I'd suggest that you comment out your main function for now and get your giveNum function working. It's really close to working, especially with Thomas' last suggestion. Then, once giveNum works, start on main. Commented Apr 25, 2017 at 5:37
  • Like, really close. Like, one character away from working. Commented Apr 25, 2017 at 5:45

2 Answers 2

1

To answer your question: because your code has syntax error.

Some additional comments:

  1. You don't need module when working with a single code file. module is for writing a library.

  2. Bindings in main :: IO () need let because it is in the IO Monad. Read more about do here

  3. Setting stdin buffering mode only once is enough.

  4. There is a sum function in Prelude.

I tried to make the code as close to the original as possible. However, this is not good Haskell code.

import System.IO

main :: IO ()
main = do
  hSetBuffering stdin LineBuffering
  n <- giveNum
  let summation = sum n
  putStrLn $ "The sum is " ++ show summation

giveNum :: IO [Int]
giveNum = do
  putStrLn "Enter num"
  num <- read <$> getLine

  if num == 0
    then return []
    else do
         rest <- giveNum
         return $ num: rest

Output:

$ runhaskell givenum.hs 
Enter num
1
Enter num
2
Enter num
3
Enter num
0
The sum is 6
Sign up to request clarification or add additional context in comments.

Comments

0

Because if you want to "assign" something in a do statement you need to put let before it, so sum = map (+) n turns into let sum = map (+) n, but as wizzup pointed out sum is already defined as a standard function and also let sum = map (+) n wont return the sum of integers in n , for that you would need foldr

let sumOfn = foldr (+) 0 n

But it would be easier to do let sumOfn = sum n Also, let f =map (+) n creates a list of functions of type Int->Int, if n is [1,2,3] then f is [(+1),(+2),(+3)].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.