0

This is a recursive data structure in Haskell, how does it work?

data Expression
     = Var Variable
     | Num Integer
     | Plus  Expression Expression
     | Minus Expression Expression
     | Times Expression Expression
     | Div   Expression Expression

data Variable = A | B

Minus (Plus (Var A)(Var B)) (VarB) : how does this expression work?

4
  • 5
    Have you read a tutorial? e.g. The Gentle Intro and Learn You a Haskell both have fine sections that should help you refine this question to something a bit more... focused. Commented Aug 21, 2012 at 10:32
  • 1
    Actually, it shouldn't work as it stands now. The type is called Expersssion (3 s's) and the recursive uses are to Experssion (2 s's) :-) Commented Aug 21, 2012 at 10:58
  • 1
    (Also, just a stylistic point: the word is "expression".) Commented Aug 21, 2012 at 11:16
  • @yatima2975 let's treat it as a typo, shall we? :) Commented Aug 21, 2012 at 12:12

1 Answer 1

1

by recursively matching its sub-parts against the types of the corresponding data constructors:

Minus    (Plus (Var A) (Var B))    (Var B)
Minus ::      Expression   ->     Expression  -> Expression

Plus           (Var A)             (Var B)
Plus  ::      Expression   ->     Expression  -> Expression

Var               A
Var   ::       Variable                       -> Expression

Var               B
Var   ::       Variable                       -> Expression

A     ::       Variable

B     ::       Variable
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.