2

I defined the tree type with record syntax way.

data Tree a = Null | Node a {lTree:: Tree a, rTree :: Tree a}

However, when I load it, it shows an error:

• Record syntax is illegal here: {lTree :: Tree a, rTree :: Tree a}
    • In the type ‘{lTree :: Tree a, rTree :: Tree a}’
      In the definition of data constructor ‘Node’
      In the data declaration for ‘Tree’
  |
2 | data Tree a = Null | Node a {lTree:: Tree a, rTree :: Tree a}
  |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2
  • 1
    Please make your post a bit more readable with formatting Commented Oct 29, 2018 at 9:45
  • By the way, it's inherently dangerous to use record syntax with multiple constructors. It makes the record accessors into partial functions. Commented Oct 29, 2018 at 15:07

1 Answer 1

10

The a should also be part of the record syntax, for example:

data Tree a = Null | Node {valTree :: a, lTree:: Tree a, rTree :: Tree a}

You can not "mix" record syntax with positional syntax as is specified by the Haskell grammar in the Haskell '98 report:

constr  -> con [!] atype1 ... [!] atypek  (arity con = k, k>=0)
        |  (btype | ! atype) conop (btype | ! atype)  (infix conop)
        |  con { fielddecl1 , ... , fielddecln }  (n>=0)
Sign up to request clarification or add additional context in comments.

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.