0

there is a question really confused me when i am running this piece of code

data Tree a = Nil | Node a (Tree a) (Tree a)
type IntTree = Tree Int
type Counter = State Int

withCounter::Int -> Counter a -> a
withCounter init f = fst(runState f init)

nextCount::Counter Int
nextCount = do
    n <- get
    put (n+1)
    return n

incTree::IntTree -> IntTree
incTree tree = withCounter 1 (IncTree' tree)
incTree' Nil = return 0
incTree' (Node l e r) = do
    newl <- incTree' l
    n <- nextCount
    newr <- incTree' r
    return (Node newl (e+n) newr)

the error is as following:

parse error on input '<-'

and it appears to be raised at line 27, that is 'n <- nextCount'

does anyone know why i am getting this error ? thanks !

3
  • What is IncTree'? It is not defined anywhere. Which State module are you using? It is best to copy and paste your code into StackOverflow, do not retype it. Commented Sep 19, 2012 at 11:04
  • i am using the Control.Monad.State module, it seems that this is all about tabs and spaces... Commented Sep 19, 2012 at 11:22
  • I think so. The simple act of writing it out again must have corrected your mistake. The advice to use spaces is good, because your text editor and haskell could potentially interpret these differently. Either pasting the whole module or just the function would have been better. The extra code should either make it compile for us or shouldn't be there because it's distracting. Parse errors don't usually involve the meaning of your code, which is why in this case you'd have been OK posting just incTree'. Commented Sep 19, 2012 at 11:27

1 Answer 1

4

As you appear not to have copied and pasted your code directly, the answer is probably that you are mixing tab characters and spaces.

Do not use tabs: indent using spaces only.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah...u got it...how silly i am...i am using notepad++ for editing hs/lhs files, it will automatically add tabs when i press the Enter button

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.