In the notes on programming language syntax page, an example parser

Two powerful List functions provided by F# are List.fold and List.foldBack. These are similar to List.reduce and List.reduceBack, but more general. Both take a binary function f, an initial value i, and a list [x1;x2;x3;...;xn]. Then List.fold returns

  (f ... (f (f (f i x1) x2) x3) ... xn)

while List.foldBack returns

  (f x1 (f x2 (f x3 ... (f xn i) ... )))

In spite of this complicated behavior, they can be implemented very simply:

  > let rec fold f a = function
    | []    -> a
    | x::xs -> fold f (f a x) xs;;

  val fold : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a

  > let rec foldBack f xs a =
      match xs with
      | []    -> a
      | y::ys -> f y (foldBack f ys a);;

  val foldBack : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b

(Note that they don’t take their arguments in the same order.)

Each of these functions can be used to implement flatten, which “flattens” a list of lists:

  let flatten1 xs = List.fold (@) [] xs

  let flatten2 xs = List.foldBack (@) xs []

For example,

  > flatten1 [[1;2];[];[3];[4;5;6]];;
  val it : int list = [1; 2; 3; 4; 5; 6]

Compare the efficiency of flatten1 xs and flatten2 xs, both in terms of asymptotic time compexity and experimentally. To make the analysis simpler, assume that xs is a list of the form [[1];[2];[3];...;[n]].

Interpreter 0 In this problem, we begin our exploration of the use of F# for language-oriented programming. You will write an F# program to evaluate arithmetic expressions written in the language given by the following context-free grammar:

  E -> n | -E | E + E | E - E | E * E | E / E | (E)

In the above, n is an integer literal, -E is the negation of E, the next four terms are the sum, difference, product, and quotient of expressions, and (E) is used to control the order of evaluation of expressions, as in the expression 3*(5-1).

Rather than working directly with the concrete syntax above, we will imagine that we have a parser that parses input into an abstract syntax tree, as is standard in real compilers. Hence your interpreter will take an input of the following discriminated union type:

  type Exp =
    Num of int
  | Neg of Exp
  | Sum of Exp * Exp
  | Diff of Exp * Exp
  | Prod of Exp * Exp
  | Quot of Exp * Exp

Note how this definition mirrors the grammar given above. For instance, the constructor Num makes an integer into an Exp, and the constructor Sum makes a pair of Exp‘s into an Exprepresenting their sum. Interpreting abstract syntax trees is much easier than trying to interpret concrete syntax directly. Note that there is no need for a constructor corresponding to parentheses, as the example given above would simply be represented by

  Prod(Num 3, Diff(Num 5, Num 1))

which represents the parse tree which looks like

Your job is to write an F# function evaluatethat takes an abstract syntax tree and returns the result of evaluating it. Most of the time, evaluating a tree will produce an integer, but we must address the possibility of dividing by zero. This could be handled by raising an exception, but instead we choose to make use of the built-in F# type

  type 'a option = None | Some of 'a

Thus evaluate will have type Exp -> int option, allowing it to return Some m in the case of a successful evaluation, and Nonein the case of an evaluation that fails due to dividing by zero. For example,

  > evaluate (Prod(Num 3, Diff(Num 5, Num 1)));;
  val it : int option = Some 12
  > evaluate (Diff(Num 3, Quot(Num 5, Prod(Num 7, Num 0))));;
  val it : int option = None

Naturally, evaluate e should use recursion to evaluate each of e‘s sub-expressions; it should also use match to distinguish between the cases of successful or failed sub-evaluations. To get you started, here is the beginning of the definition of evaluate:

  let rec evaluate = function
  | Num n -> Some n
  | Neg e -> match evaluate e with
       | ...
Order a unique copy of this paper
(550 words)

Approximate price: $22

Place Order
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

We value our customers and so we ensure that what we do is 100% original..
With us you are guaranteed of quality work done by our qualified experts.Your information and everything that you do with us is kept completely confidential.

Zero-plagiarism guarantee

The Product ordered is guaranteed to be original. Orders are checked by the most advanced anti-plagiarism software in the market to assure that the Product is 100% original. The Company has a zero tolerance policy for plagiarism.

Read more

Free-revision policy

The Free Revision policy is a courtesy service that the Company provides to help ensure Customer’s total satisfaction with the completed Order. To receive free revision the Company requires that the Customer provide the request within fourteen (14) days from the first completion date and within a period of thirty (30) days for dissertations.

Read more

Privacy policy

The Company is committed to protect the privacy of the Customer and it will never resell or share any of Customer’s personal information, including credit card data, with any third party. All the online transactions are processed through the secure and reliable online payment systems.

Read more

Fair-cooperation guarantee

By placing an order with us, you agree to the service we provide. We will endear to do all that it takes to deliver a comprehensive paper as per your requirements. We also count on your cooperation to ensure that we deliver on this mandate.

Read more

Calculate the price

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
× WhatsApp us for help