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]]`.

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 `Exp`representing 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 `evaluate`that 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 `None`in 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 | ...

Try it now!

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Academic Solvers has put together a group of highly qualified writers with a wide range of expertise in the online writing community. Our goal is to become your one-stop shop for all academic and online writing needs. Take a look at our fantastic service below!

Editing

At Academic SolversEditing your research paper could prove to be difficult especially amidst the tight schedules that students have. At academicsolvers.net, we offer research paper editing services. The services include proofreading and use of online platforms such as Grammarly and Turnitin to address any concerns that may arise within the paper. Get our services and you will have satisfactory results.

Research Paper

Custom Research Paper

Writing a research paper could be quite hectic especially if it is your first time. Identifying the research topic, finding the references and interpreting information could pose significant challenges. However, this should not be issues of concern anymore. At academicsolvers.net, we offer solutions to research papers. We deliver plagiarism free and quality research papers that are customized to suit your needs.

Editing

Online Proofreading

Despite writing expertise and skills, you are never guaranteed of error free papers. Academic solvers’ online proofreading services leaves no mistake unaddressed. We go through your paper using a variety of online tools that help to ensure that we edit your paper and make it error free. We have collected a group of experts hence can guarantee quality proofreading services.

Coursework

Cheap Research Papers

Do you have difficulties writing your research paper? Are you afraid of the cost of hiring a professional to write your research papers? Worry no more. At academicsolvers.net we address both of your problems. We are equipped with a large number of professional writers. Second, we offer our research paper writing services at cheap prices that are largely affordable. Don’t miss this chance. Order our cheap research papers today.

Coursework

College Assignment

College assignment could be one of the biggest challenges to students especially working with tight schedules. Academicsolvers.net acts to bridge this gap existing between getting satisfactory grades and meeting deadlines. Our college assignment professionals provide students with quality, plagiarism free and confidential assignment help that helps them to achieve desired grades and outcomes.

Coursework

Essay Help

Essays are some of the most critical elements in the academic life of a student. At academic solvers we prioritize providing quality essays. As such, our professional ensure that they address issues of grammar, structure and plagiarism to provide essays that meet the need of the client. Here, we focus on developing content that aims at addressing the subject in question through the expertise of our professional and experienced writers. Try our services and we guarantee succes.