1

The input to the code is numbers written in words "one" "two" etc. the output is the sum of a all numbers inputed when "sum" or "end" is entered. "end" also exits the program.

This is the code that I've written.

main = addfunc 0


addfunc :: Int -> IO ()
addfunc l = do
 {
  input <- getLine;
  let n = myStrToI input
  ; if n == -2 
    then do {
          putStrLn("The final sum is: " ++ show (l));
          return ();            
          }
    else if n == -1 
     then 
           putStrLn("The current sum is: " ++ show(l));
           addfunc l;     <--Error Here       
    else  addfunc (n+l)
 }

myStrToI :: [Char] -> Int
myStrToI l
  | l == "zero" = 0
  | l == "one" = 1
  | l == "two" = 2
  | l == "three" = 3
  | l == "four" = 4
  | l == "five" = 5
  | l == "six" = 6
  | l == "seven" = 7
  | l == "eight" = 8
  | l == "nine" = 9
  | l == "sum" = -1
  | l == "end" = -2

The error I get is

"error: parse error on input 'addfunc'

17. addfunc l;"

I've tried changing the amount of spaces or removing the ';' in the line above, but I still get an error at that place. Is this error because the indentation of if else blocks? I'm beginner to Haskell and don't really understand where the error is.

3
  • 3
    You're missing a do keyword on line 15 Commented Sep 14, 2022 at 18:34
  • 4
    Can you please get used to standard indented style without { } ;? Yes, Haskell allows C-like syntax too, but it's not really supposed to be used in human-written code. Commented Sep 14, 2022 at 19:13
  • 1
    If you do want to use the { .. ; .. ; .. }-style, you should really use a consistent indentation. The one above is completely misleading. Just because indentation does no longer matter to the compiler is not a good reason: in C,Java, etc. it does matter yet it is still a must-have for humans. Also note that ; is a separator, not a terminator, hence there must be no ; just before the }. Commented Sep 14, 2022 at 20:43

1 Answer 1

4

If you use { } (which you probably shouldn't), then did disables all indentation sensitivity inside. So your code parses just the same as

addfunc :: Int -> IO ()
addfunc l = do
 {
  input <- getLine;
  let n = myStrToI input;
  if n == -2
  then do {putStrLn("The final sum is: " ++ show (l)); return ()}
  else if n == -1 
  then putStrLn("The current sum is: " ++ show(l));
  addfunc l
  else addfunc (n+l)
 }

So what does the compiler make of that? Well, let's also move the semicolons around a bit:

addfunc :: Int -> IO ()
addfunc l = do
 { input <- getLine
 ; let n = myStrToI input
 ; if n == -2
   then do {putStrLn("The final sum is: " ++ show (l)); return ()}
   else if n == -1 
   then putStrLn("The current sum is: " ++ show(l))
 ; addfunc l
   else addfunc (n+l)
 }

Whoops. The if n == -1 is never finished, instead you skip to the next line in the outermost do block, and there's then a lone else there.

What you actually need is another do for that if branch. Preferrably use indentation without braces:

addfunc :: Int -> IO ()
addfunc l = do
  input <- getLine
  let n = myStrToI input
  if n == -2 
   then do
     putStrLn("The final sum is: " ++ show (l))
     return ()
   else if n == -1 
   then do
     putStrLn $ "The current sum is: " ++ show l
     addfunc l
   else
     addfunc (n+l)
 

Actually I'd prefer using a case construct instead of those ifs:

  ...
  let n = myStrToI input
  case n of
   -2 -> do
     putStrLn("The final sum is: " ++ show (l))
     return ()
   -1 -> do
     putStrLn $ "The current sum is: " ++ show l
     addfunc l
   _ ->
     addfunc (n+l)
Sign up to request clarification or add additional context in comments.

1 Comment

In the OP code there are a few additional errors. One is do { putStrLn (...) ; return (); } where 1) the last ; should not be there, 2) the return () does nothing anyway here (OK, not strictly a bug, but...). Also, another extra ; after addfunc l which makes no sense.

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.