-1

I'm working on a function to read a list for user input and then create a tree from it. Here's the code

readList : IO (Maybe BSTree a)
readList = do x <- getLine
              if all isDigit (unpack input)
                  then do (_ ** xs) <- readList
                      pure ( listToTree (cast x)::xs )
                  else pure Nothing

This is the type definition of listToTree

listToTree : Ord a => List a -> BSTree a

In type-checking readList, I get an error of "unexpected pure" in the pure ( listToTree (cast x)::xs ) line. Is this an indentation issue? Why isn't the pure keyword taking here?

Oneiros
  • 143
  • 1
  • 13
  • 1
    can you make this into an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) please, so it's easier to debug – joel Aug 06 '20 at 16:20
  • for example, what's `input`? what's `BSTree`? – joel Aug 06 '20 at 16:23
  • 1
    Align `pure ...` with `(_ ** xs) ...` above it to get rid of the syntax error. (Though there should be type errors as well.) – xash Aug 06 '20 at 23:11
  • from the [docs](http://docs.idris-lang.org/en/latest/tutorial/typesfuns.html?#do-notation): "Indentation is significant — each statement in the do block must begin in the same column". Though the docs don't explain what a statement is and whether `do` is part of it – joel Aug 07 '20 at 07:09

1 Answers1

1

Indentation is very important when working in Idris and similar languages. I would recommend reading some standard library code to see the conventions used around indentation.

The following will parse:

readList : IO (Maybe (BSTree a))
readList = do
  x <- getLine
  if all isDigit (unpack input)
  then do
    (_ ** xs) <- readList
    pure (listToTree (cast x) :: xs)
  else
    pure Nothing
Brian McKenna
  • 45,528
  • 6
  • 61
  • 60