Ben Fossen
Ben Fossen

Reputation: 1007

Constructors in Haskell

This is an excerpt from homework. "Prog This datatype should have only one constructor and be used for representing programs of the form:

read vin1 ; read vin2 ; < some statements > write vout2 ;

This constructor, which can also be named Prog, takes a string for the names of the two input and one output variable."

Define expProg to be of type Prog and to be the abstract syntax representation of the program on the left above. This should be done as follows:

expProg = Prog "x" "y" <some statements> "z"

I am new to Haskell and am confused by this.

I made a constructor like this. However this doesnt seem right.

data Prog = Prog String String String 
  deriving (Show,Eq)

Could anyone explain whats going on here? I do not understand how to make this constructor. Here is the data type i made for statements:

 data Stmt = Assing String Expr
      | WhileLoop Expr Stmt
      | Ifthen Expr Stmt
      | IfthenElse Expr Stmt Stmt
      | Composition [Stmt]

Upvotes: 0

Views: 126

Answers (1)

dflemstr
dflemstr

Reputation: 26177

You said you needed to include "some statements" in the constructor as well. Currently, you only have room for the two input variables and the output variable. Simply add another field for the statements, for example like this:

data Prog = Prog String String Stmt String
--                                  ^ output variable
--                             ^ some statements
--                       ^ input variable 2
--               ^ input variable 1

Now you can use it like so:

expProg = Prog "x" "y" (Composition [Assing "x" (...some expression...),
                                     Assing "z" (...some expression...)])  "z"

(Note that it's actually spelled Assign, not Assing)

Upvotes: 6

Related Questions