SlideShare a Scribd company logo
In the
Notes on Programming Language Syntax
page, an example parser for a simple language is given, using C
syntax. Write the parser using F#, but you may only use
functional programming and immutable date. Create the list of
tokens as a discriminated union, which (in the simplest case)
looks like an enumeration.
type TERMINAL =
IF|THEN|ELSE|BEGIN|END|PRINT|SEMICOLON|ID|EOF
With this type declared, you can use the terminals like you
would use enumerated values in Java.
Use immutable data. The C-code example uses mutable data.
Pass the program into the start symbol function. Pass the input
yet to be processed to each non-terminal function.
The main function might look like this:
let test_program program =
let result = program |> S
match result with
| [] -> failwith "Early termination or missing EOF"
| x::xs -> if x = EOF then accept() else error()
You do not have to parse input strings. Assume that the parsing
has been done. Pass a list of tokens that represent a program
into the start symbol. Try these program examples:
[IF;ID;THEN;BEGIN;PRINT;ID;SEMICOLON;PRINT;ID;END;
ELSE;PRINT;ID;EOF]
[IF;ID;THEN;IF;ID;THEN;PRINT;ID;ELSE;PRINT;ID;ELSE;B
EGIN;PRINT;ID;END;EOF]
Causes error:
[IF;ID;THEN;BEGIN;PRINT;ID;SEMICOLON;PRINT;ID;SEMI
COLON;END;ELSE;PRINT;ID;EOF]
Print an accept message when the input is valid and completely
consumed. Generate appropriate error messages for incorrect
symbols, not enough input, and too much input.
Once you have the parser recognizing input, generate a parse
tree using a discriminated type.
Implement a parser using functional programming and
immutable data for the unambiguous grammar for arithmetic
expressions, from the
Notes on Programming Language Syntax.
E -> E + T | E - T | T
T -> T * F | T / F | F
F -> i | (E)
Use the suggestion in the notes to get around the fact that this
grammar appears to need more than one lookahead token.
Once you have the parser recognizing input, generate a parse
tree using a discriminated type.
Recall that an F# function that takes two arguments can be
coded in either uncurried form (in which case it takes a pair as
its input) or curried form (in which case it takes the first
argument and returns a function that takes the second
argument). In fact it is easy to convert from one form to the
other in F#. To this end, define an F# function
curry f
that converts an uncurried function to a curried function, and
an F# function
uncurry f
that does the opposite conversion. For example,
> (+);;
val it : (int -> int -> int) =
[email protected]
>
> let plus = uncurry (+);;
val plus : (int * int -> int)
> plus (2,3);;
val it : int = 5
> let cplus = curry plus;;
val cplus : (int -> int -> int)
> let plus3 = cplus 3;;
val plus3 : (int -> int)
> plus3 10;;
val it : int = 13
What are the types of
curry
and
uncurry
?
Given vectors
u = (u
1
, u
2
,..., u
n
)
and
v = (v
1
, v
2
,..., v
n
)
, the
inner product
of
u
and
v
is defined to be
u
1
*v
1
+ u
2
*v
2
+ ... + u
n
*v
n
. Write a curried F# function
inner
that takes two vectors represented as
int list
and returns their inner product.
Throw an exception if the lists do not have the same length.
Do not use any built-in or borrowed functions. Write it from
scratch.
Use tail recursion.
> inner [1;2;3] [4;5;6];; val it : int = 32
Given an
m
-by-
n
matrix
A
and an
n
-by-
p
matrix
B
, the product of
A
and
B
is an
m
-by-
p
matrix whose entry in position (
i,j
) is the inner product of row
i
of
A
with column
j
of
B
. For example,
/ 0 1 
/ 1 2 3  * | 3 2 | = / 9 11
4 5 6 /  1 2 /  21 26 /
Write an uncurried F# function to do matrix multiplication:
> multiply ([[1;2;3];[4;5;6]], [[0;1];[3;2];[1;2]]);;
val it : int list list = [[9; 11]; [21; 26]]
Assume that the dimensions of the matrices are appropriate.
Hint
: Use
transpose
(from
Homework 1
),
inner
, and
List.map
.
Evaluate the asymptotic time complexity of this function:
let rec oddeven = function
| [] -> []
| x::xs -> if x % 2 = 0
then oddeven xs @ [x]
else x :: oddeven xs
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
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
| ...
Record
Create a record type for Name, Credits and GPA.
Create a record instance with the values "Jones", 109, 3.85.
Discriminated Union
Create a discriminated union for Coordinates that can be a
Tuple, Threeple or Fourple that represent tuples of size two,
three and four. The type for the union should be polymorphic.
Instantiate a Tuple of integers, a Threeple of floats and a
Fourple of strings.
Create a function that has a parameter of a binary function and
Coordinate. Apply the function to the Coordinate like
List.reduce. Call the function with (+) for each of the
Coordinates in part (b).
Binary Tree
Write a function that searches for a value in a binary tree and
then removes that node. Be sure to handle all of these cases:
The value is not in the tree.
Both children are Lf.
One child node is a Br the other is a Lf.
Both children are Br nodes.
Draw a memory diagram with before and after views for the
case where both children are Br nodes.
In the Notes on Programming Language Syntax page, an example par.docx

More Related Content

Similar to In the Notes on Programming Language Syntax page, an example par.docx (20)

PDF
F# code, code below If i = 1 the branch jumps to line 2, so the pr.pdf
alphaagenciesindia
 
PPT
F# and the DLR
Richard Minerich
 
PDF
The Fuss about || Haskell | Scala | F# ||
Ashwin Rao
 
PPTX
F# Presentation
mrkurt
 
PPTX
Functional Programming Fundamentals
OleksiyTereshchenko
 
PPTX
Intro to F#
Kristian Hellang
 
PDF
Scala Functional Patterns
league
 
PDF
Using Language Oriented Programming to Execute Computations on the GPU
Skills Matter
 
ODP
F# 101
Chris Alcock
 
PDF
F# delight
priort
 
PPTX
Intro f# functional_programming
Mauro Ghiani
 
PPTX
Sharper tools with F#
Kevin Avignon
 
PPTX
Reasonable Code With Fsharp
Michael Falanga
 
PPTX
F sharp _vs2010_beta2
Eriawan Kusumawardhono
 
PDF
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
Johannes Hoppe
 
ODP
Progressive f# tutorials nyc dmitry mozorov & jack pappas on code quotations ...
Skills Matter
 
PDF
Why Haskell Matters
romanandreg
 
PPTX
Functional programming in f sharp
chribben
 
PPTX
What The F#
RTigger
 
PPTX
What the math geeks don't want you to know about F#
Kevin Hazzard
 
F# code, code below If i = 1 the branch jumps to line 2, so the pr.pdf
alphaagenciesindia
 
F# and the DLR
Richard Minerich
 
The Fuss about || Haskell | Scala | F# ||
Ashwin Rao
 
F# Presentation
mrkurt
 
Functional Programming Fundamentals
OleksiyTereshchenko
 
Intro to F#
Kristian Hellang
 
Scala Functional Patterns
league
 
Using Language Oriented Programming to Execute Computations on the GPU
Skills Matter
 
F# 101
Chris Alcock
 
F# delight
priort
 
Intro f# functional_programming
Mauro Ghiani
 
Sharper tools with F#
Kevin Avignon
 
Reasonable Code With Fsharp
Michael Falanga
 
F sharp _vs2010_beta2
Eriawan Kusumawardhono
 
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
Johannes Hoppe
 
Progressive f# tutorials nyc dmitry mozorov & jack pappas on code quotations ...
Skills Matter
 
Why Haskell Matters
romanandreg
 
Functional programming in f sharp
chribben
 
What The F#
RTigger
 
What the math geeks don't want you to know about F#
Kevin Hazzard
 

More from mecklenburgstrelitzh (20)

DOCX
Discussion - Week 3Elements of the Craft of WritingThe narra.docx
mecklenburgstrelitzh
 
DOCX
Discussion - Microbial ClassificationGive names of bacteria in.docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 7) What are the common challenges with which se.docx
mecklenburgstrelitzh
 
DOCX
Discussion - Big Data Visualization toolsSeveral Big Data Visu.docx
mecklenburgstrelitzh
 
DOCX
Discussion - 1 Pick 2 different department team members and descri.docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 7) What are the common challenges with which .docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 7) What are the common challenges with whic.docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 6) List and briefly describe the nine-step .docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 5) What is the relationship between Naïve Bayes.docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 4) What are the privacy issues with data mini.docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 3) Why are the originalraw data not readily us.docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 5) What is the relationship between Naïve B.docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 10 in the textbook or see the ppt) For ea.docx
mecklenburgstrelitzh
 
DOCX
Discussion (Chapter 1) Compare and contrast predictive analytics wi.docx
mecklenburgstrelitzh
 
DOCX
Discussion (400 words discussion + 150 words student response)Co.docx
mecklenburgstrelitzh
 
DOCX
Discussion (150-200 words) Why do you think so much emphasis is pla.docx
mecklenburgstrelitzh
 
DOCX
discussion (11)explain the concept of information stores as th.docx
mecklenburgstrelitzh
 
DOCX
Discussion #5 How progressive was the Progressive EraThe Progres.docx
mecklenburgstrelitzh
 
DOCX
Discussion #4, Continued Work on VygotskyA. Why is it important .docx
mecklenburgstrelitzh
 
DOCX
Discussion #4 What are the most common metrics that make for an.docx
mecklenburgstrelitzh
 
Discussion - Week 3Elements of the Craft of WritingThe narra.docx
mecklenburgstrelitzh
 
Discussion - Microbial ClassificationGive names of bacteria in.docx
mecklenburgstrelitzh
 
Discussion (Chapter 7) What are the common challenges with which se.docx
mecklenburgstrelitzh
 
Discussion - Big Data Visualization toolsSeveral Big Data Visu.docx
mecklenburgstrelitzh
 
Discussion - 1 Pick 2 different department team members and descri.docx
mecklenburgstrelitzh
 
Discussion (Chapter 7) What are the common challenges with which .docx
mecklenburgstrelitzh
 
Discussion (Chapter 7) What are the common challenges with whic.docx
mecklenburgstrelitzh
 
Discussion (Chapter 6) List and briefly describe the nine-step .docx
mecklenburgstrelitzh
 
Discussion (Chapter 5) What is the relationship between Naïve Bayes.docx
mecklenburgstrelitzh
 
Discussion (Chapter 4) What are the privacy issues with data mini.docx
mecklenburgstrelitzh
 
Discussion (Chapter 3) Why are the originalraw data not readily us.docx
mecklenburgstrelitzh
 
Discussion (Chapter 5) What is the relationship between Naïve B.docx
mecklenburgstrelitzh
 
Discussion (Chapter 10 in the textbook or see the ppt) For ea.docx
mecklenburgstrelitzh
 
Discussion (Chapter 1) Compare and contrast predictive analytics wi.docx
mecklenburgstrelitzh
 
Discussion (400 words discussion + 150 words student response)Co.docx
mecklenburgstrelitzh
 
Discussion (150-200 words) Why do you think so much emphasis is pla.docx
mecklenburgstrelitzh
 
discussion (11)explain the concept of information stores as th.docx
mecklenburgstrelitzh
 
Discussion #5 How progressive was the Progressive EraThe Progres.docx
mecklenburgstrelitzh
 
Discussion #4, Continued Work on VygotskyA. Why is it important .docx
mecklenburgstrelitzh
 
Discussion #4 What are the most common metrics that make for an.docx
mecklenburgstrelitzh
 

Recently uploaded (20)

PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Workbook de Inglés Completo - English Path.pdf
shityouenglishpath
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Workbook de Inglés Completo - English Path.pdf
shityouenglishpath
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Introduction presentation of the patentbutler tool
MIPLM
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 

In the Notes on Programming Language Syntax page, an example par.docx

  • 1. In the Notes on Programming Language Syntax page, an example parser for a simple language is given, using C syntax. Write the parser using F#, but you may only use functional programming and immutable date. Create the list of tokens as a discriminated union, which (in the simplest case) looks like an enumeration. type TERMINAL = IF|THEN|ELSE|BEGIN|END|PRINT|SEMICOLON|ID|EOF With this type declared, you can use the terminals like you would use enumerated values in Java. Use immutable data. The C-code example uses mutable data. Pass the program into the start symbol function. Pass the input yet to be processed to each non-terminal function. The main function might look like this: let test_program program = let result = program |> S match result with | [] -> failwith "Early termination or missing EOF" | x::xs -> if x = EOF then accept() else error() You do not have to parse input strings. Assume that the parsing has been done. Pass a list of tokens that represent a program into the start symbol. Try these program examples: [IF;ID;THEN;BEGIN;PRINT;ID;SEMICOLON;PRINT;ID;END; ELSE;PRINT;ID;EOF] [IF;ID;THEN;IF;ID;THEN;PRINT;ID;ELSE;PRINT;ID;ELSE;B EGIN;PRINT;ID;END;EOF]
  • 2. Causes error: [IF;ID;THEN;BEGIN;PRINT;ID;SEMICOLON;PRINT;ID;SEMI COLON;END;ELSE;PRINT;ID;EOF] Print an accept message when the input is valid and completely consumed. Generate appropriate error messages for incorrect symbols, not enough input, and too much input. Once you have the parser recognizing input, generate a parse tree using a discriminated type. Implement a parser using functional programming and immutable data for the unambiguous grammar for arithmetic expressions, from the Notes on Programming Language Syntax. E -> E + T | E - T | T T -> T * F | T / F | F F -> i | (E) Use the suggestion in the notes to get around the fact that this grammar appears to need more than one lookahead token. Once you have the parser recognizing input, generate a parse tree using a discriminated type. Recall that an F# function that takes two arguments can be coded in either uncurried form (in which case it takes a pair as its input) or curried form (in which case it takes the first argument and returns a function that takes the second argument). In fact it is easy to convert from one form to the other in F#. To this end, define an F# function curry f that converts an uncurried function to a curried function, and an F# function uncurry f that does the opposite conversion. For example, > (+);;
  • 3. val it : (int -> int -> int) = [email protected] > > let plus = uncurry (+);; val plus : (int * int -> int) > plus (2,3);; val it : int = 5 > let cplus = curry plus;; val cplus : (int -> int -> int) > let plus3 = cplus 3;; val plus3 : (int -> int) > plus3 10;; val it : int = 13 What are the types of curry and uncurry ? Given vectors u = (u 1 , u 2 ,..., u n
  • 4. ) and v = (v 1 , v 2 ,..., v n ) , the inner product of u and v is defined to be u 1 *v 1 + u 2 *v 2 + ... + u n *v n . Write a curried F# function inner that takes two vectors represented as int list and returns their inner product. Throw an exception if the lists do not have the same length. Do not use any built-in or borrowed functions. Write it from scratch.
  • 5. Use tail recursion. > inner [1;2;3] [4;5;6];; val it : int = 32 Given an m -by- n matrix A and an n -by- p matrix B , the product of A and B is an m -by- p matrix whose entry in position ( i,j ) is the inner product of row i of A with column j of B . For example, / 0 1 / 1 2 3 * | 3 2 | = / 9 11
  • 6. 4 5 6 / 1 2 / 21 26 / Write an uncurried F# function to do matrix multiplication: > multiply ([[1;2;3];[4;5;6]], [[0;1];[3;2];[1;2]]);; val it : int list list = [[9; 11]; [21; 26]] Assume that the dimensions of the matrices are appropriate. Hint : Use transpose (from Homework 1 ), inner , and List.map . Evaluate the asymptotic time complexity of this function: let rec oddeven = function | [] -> [] | x::xs -> if x % 2 = 0 then oddeven xs @ [x] else x :: oddeven xs Two powerful List functions provided by F# are List.fold and List.foldBack . These are similar to List.reduce and
  • 7. 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
  • 8. | [] -> 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
  • 9. 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
  • 10. | 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
  • 11. 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
  • 12. | Num n -> Some n | Neg e -> match evaluate e with | ... Record Create a record type for Name, Credits and GPA. Create a record instance with the values "Jones", 109, 3.85. Discriminated Union Create a discriminated union for Coordinates that can be a Tuple, Threeple or Fourple that represent tuples of size two, three and four. The type for the union should be polymorphic. Instantiate a Tuple of integers, a Threeple of floats and a Fourple of strings. Create a function that has a parameter of a binary function and Coordinate. Apply the function to the Coordinate like List.reduce. Call the function with (+) for each of the Coordinates in part (b). Binary Tree Write a function that searches for a value in a binary tree and then removes that node. Be sure to handle all of these cases: The value is not in the tree. Both children are Lf. One child node is a Br the other is a Lf. Both children are Br nodes. Draw a memory diagram with before and after views for the case where both children are Br nodes.