Define a recursive function normAdd

Assignment Help Programming Languages
Reference no: EM132231825

Assignment -

This is a programming assignment in F#, with some additional non-programming questions. You can do it alone or in a team of two people. In the latter case, you are responsible for contacting other students and form a team.

Download the accompanying F# source file hw.fsx and enter your solutions in it where indicated. When you are done, submit it as instructed on Piazza.

Each of your answers must be free of static errors. You may receive no credit for problems whose code contains syntax or type errors.

In the following problems do not use any mutable variables (i.e., variables declared with let mutable) or any mutable predefined types such as references, mutable records, arrays, and so on. Define your functions recursively as needed and take advantage of the match operator.

1. Extending the expression language and its interpreter

File hw2.fsx contains the datatype expr

type  expr  =

| Const of int

| Op of string * expr * expr

| IfElse of expr * expr * expr

meant to encode the abstract syntax of a simple expression language like the one discussed in class. A datatype value of the form (Const n) encodes the integer constant n, while a datatype value of the form Op ("+", t1, t2), say, encodes the expression e1 + e2 if each ti in turn encodes the expression ei.

1. Define an eval function similar to the one discussed in class that evaluates expr values to an integer value as expected and supports the binary operator names "+", "-", "*", "max", "min", "=" and ">", respectively with the following behavior:

  • "+", "-", "*" are as seen in class;
  • "max" denotes the function that returns the largest of its arguments;
  • "min" denotes the function that returns the smallest of its arguments;
  • "=" denotes the function that returns 1 when its arguments have the same value, and returns 0 otherwise;
  • ">" denotes the function that returns 1 when the value of its first argument is greater than the value of its second argument, and returns 0 otherwise.

The function eval fails with failwith in case of unrecognized binary operators and (for now) with expressions of the form IfElse(e, e1, e2).

2. Now we would like to extend our interpreter to conditional expressions of the form IfElse(e, e1, e2), giving them the same semantics as the corresponding Java expression e ? e1 : e2 or F#'s expression if e then e1 else e2.

Extend eval into a new function eval2 that behaves like eval except that it handles IfElse expressions as follows: IfElse(e, e1, e2) evaluates to the value of e1 if e evaluates to a non-zero value, and to the value of e2 otherwise. Your extension should be such that e1 and e2 are evaluated only as needed, after e has been evaluated.

3. Using pattern matching, define a recursive function normAdd : expr -> expr that normalizes additions in the input expression by returning an equivalent one where any nested applications of "+" are right-associated.

2. An expression language with variables

In this problem we will consider a variation of the datatype expr where we represent operators with an enumeration type instead of the string type and we add support for variables, to represent arithmetic expressions with variables.

type oper1 = Neg | Not

type oper2 = Add | Mul | Sub | Gt | Eq | Or

type aexpr =

| C of int

| V of string

| Op1 of oper1 * aexpr

| Op2 of oper2 * aexpr * aexpr

With the two datatypes above, an expression like x * (y + 3), say, is represented as

Op2(Mul, Op1(Neg, Var "x"), Op2(Add, Var "y", C 3)) .

Similarly, an expression like :(x > 4 y), say, is represented as

Op1(Not, Op2(Gt, Var "x", Op2(Sub, C 4, Var "y"))) .

1. Assuming the usual precedence rules between operators and treating '+' and binary ' ' as left-associative operators, define three F# variables e1, e2 and e3 of type aepr whose value corresponds to the expressions z (v + w), 4 * (z - 2 * w - v), and x + y + v + w * z, respectively.

2. Write an interpreter aeval: env -> aexpr -> int for programs written as aexpr expressions. Function aeval takes an environment e and an expression a as input and returns the value of a in that environment.

The function should evaluate variables in a as specified in e, failing with failwith if a contains a variable that is undefined in e. It should interpret Neg, Add, Mul, and Sub respectively as integer negation, addition, multiplication and subtraction. It should interpret the remaining operators as the Boolean operators in C. Namely,

  • Op2(Gt, e1, e2) evaluates to 1 if e1 evaluates to a number greater than the number e2 evaluates to, and it evaluates to 0 otherwise.
  • Op1(Not, e) evaluates to 1 if e evaluates to 0 and evaluates to 1 otherwise;
  • Op2(Eq, e1, e2) evaluates to 1 if e1 and e2 evaluate to the same number, and evaluates to 0 otherwise;
  • Op2(Or, e1, e2) evaluates to 1 if e1 or e2 evaluates to 1, and evaluates to 0 otherwise.

You can use the provided function lookup to lookup the value of a variable in an environment.

3. Write an F# function simplify: aexpr -> aexpr to perform some simple expression simplifications. Such simplifications are applied for instance in optimizing compilers. The simplify function relies on standard properties of arithmetic to reduce an expression to a simpler, but equivalent, one and to eliminate some of the Boolean operators. In particular, it should use the following simplification rules for an arbitrary expressions e, where V is logical disjunction (or) and ¬ is logical negation (not):

0 + e → e

e + 0 → e

e - 0 → e

1 * e → e

e * 1 → e

0 * e → 0

e * 0 → 0

e - e → 0

-(-e) → e

e V e → e

¬(¬e) → e

This means, for instance, that it should reduce x + 0, 1 * x, and (1 + 0) * (x + 0) all to x.

Your implementation should be such that the result of simplify is an expression that cannot be simplified further by it. That is, it should be such that, for all expressions e, (simplify (simplify e)) = (simplify e).

Notes: Pattern matching is your friend. Don't forget cases where you cannot simplify anything. Repeated (recursive) passes of simplify might be needed to produce an expression that cannot be simplified further.

4. Write an F# function toString: aexpr -> string to format expressions as strings, with the binary operators written in infix format. For instance, it may format Sub(V "x", C 34) as the string "x - 34". For simplicity, you may put parentheses around any subexpressions, even when they are superfluous according to the standard precedence rules for arithmetic operators. Use the predefined function string to convert an integer value to its string representation.

Hint: toString has very much the same structure as an eval function, although it needs no environment argument because it uses variables names, not variable values.

Attachment:- Assignment Files.rar

Reference no: EM132231825

Questions Cloud

Compute the annual payment : the annual paymentYou won the lottery for 300 million. You can take a lump sum today in the amount of $92,495,600. Compute the annual payment
What is the entry for payment of the invoice : On September 10, the invoice was paid when the exchange rate was $1.75 = 1 pound. What is the entry for payment of the invoice
Impacts and consequences of the diversity : Explain how the various postmodern critical "criminologies," exposed the repressive practices of domination, inequality, and inhumanity found
Create a database driven website : Prepare a simple report for each meeting that shows how you are doing against the project plan, and whether the plan needs updating to reflect any changes
Define a recursive function normAdd : Using pattern matching, define a recursive function normAdd : expr -> expr that normalizes additions in the input expression
Difference between absorption costing and variable costing : What is the basic difference between absorption costing and variable costing - What is the company's break-even point in terms of units sold
Gather current data on the markets people and economy : The MIR requires teams to gather current, or the most recently available, data on the market's people, economy, government, and technological status
Presenting about the integration in americas : As discussed, the groups will be presenting about the following organization (regional/ global) Group - Integration in Americas (CAFTA-DR; Andean Community
What should firms do to better prepare for the two scenarios : From a resource-based view, what should firms do to better prepare for the two scenarios? ON ETHICS: From an institution-based view.

Reviews

len2231825

2/11/2019 4:42:46 AM

This is a programming assignment in F#, with some additional non-programming questions. You can do it alone or in a team of two people. In the latter case, you are responsible for contacting other students and form a team. Download the accompanying F# source file hw.fsx and enter your solutions in it where indicated. When you are done, submit it as instructed on Piazza. Each of your answers must be free of static errors. You may receive no credit for problems whose code contains syntax or type errors. Pay close attention to the specification of each problem and the restrictions imposed on its solution.

len2231825

2/11/2019 4:42:40 AM

Solutions ignoring the restrictions may receive only partial credit or no credit at all. In the following problems do not use any mutable variables (i.e., variables declared with let mutable) or any mutable predefined types such as references, mutable records, arrays, and so on. Define your functions recursively as needed and take advantage of the match operator. You do not need, and should not use, any F# library functions unless instructed otherwise. However, you can use your own auxiliary functions if you prefer. Do not worry about the efficiency of your solution. Strive instead for cleanness and simplicity. Unnecessarily complicated code may not receive full credit. Do the assigned readings before starting attempting this assignment. If you do not, you might find the assignment exceedingly hard. Be sure to review the syllabus for details about this course's cheating policy.

Write a Review

Programming Languages Questions & Answers

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd