Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
pp:l04xx [2020/03/20 21:33] pdmatei created |
pp:l04xx [2020/04/07 15:11] (current) pdmatei |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ======= Programming with ADTs ======= | + | ======= 6. Programming with ADTs ======= |
| 1. A **dictionary** is a collection of key-value pairs, which we can represent in Haskell as a list of pairs ''(String,Integer)'' where ''String'' is the key type and ''Integer'' is the value type: | 1. A **dictionary** is a collection of key-value pairs, which we can represent in Haskell as a list of pairs ''(String,Integer)'' where ''String'' is the key type and ''Integer'' is the value type: | ||
| Line 25: | Line 25: | ||
| </code> | </code> | ||
| - | 4. Implement a function which takes a dictionary of program variables, a program expression ''PExpr'', and evaluates it. For instance: ''eval_pexpr [("x",2),("y",1)] ((Var "x") :+: (Var "y"))'' returns 3. | + | 4. Implement a function which takes a dictionary of program variables, a program expression ''PExpr'', and evaluates it. For instance: ''eval_pexpr [("x",2),("y",1)] ( (Var "x") :+: (Var "y") )'' returns 3. |
| <code haskell> | <code haskell> | ||
| eval_pexpr :: Dict -> PExpr -> Integer | eval_pexpr :: Dict -> PExpr -> Integer | ||
| Line 61: | Line 61: | ||
| Begin Prog Prog | -- <p> <p'> | Begin Prog Prog | -- <p> <p'> | ||
| While BExpr Prog | -- while (<expr>) { <p> } | While BExpr Prog | -- while (<expr>) { <p> } | ||
| - | If BExpr Prog Prog -- if (<expr>) { <p> } else { <p'> } | + | If BExpr Prog Prog -- if (<expr>) { <p> } else { <p'> } |
| - | + | ||
| - | -- deriving Show | + | |
| - | -- ex7: modify show_p so that program and boolean expressions are also displayed | ||
| show_p :: Prog -> String | show_p :: Prog -> String | ||
| show_p (PlusPlus v) = v++"++;\n" | show_p (PlusPlus v) = v++"++;\n" | ||
| Line 85: | Line 82: | ||
| x = x + 1 | x = x + 1 | ||
| } | } | ||
| + | </code> | ||
| 8. Define a function ''eval'' which takes a dictionary and a program, and evaluates the program under the given dictionary. The function will return an updated dictionary. For instance: | 8. Define a function ''eval'' which takes a dictionary and a program, and evaluates the program under the given dictionary. The function will return an updated dictionary. For instance: | ||