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
Ad

More Related Content

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

Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
Dmitri Nesteruk
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
Nitesh Dubey
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
Radhakrishnan Chinnusamy
 
advanced python for those who have beginner level experience with python
advanced python for those who have beginner level experience with pythonadvanced python for those who have beginner level experience with python
advanced python for those who have beginner level experience with python
barmansneha1204
 
Advanced Python after beginner python for intermediate learners
Advanced Python after beginner python for intermediate learnersAdvanced Python after beginner python for intermediate learners
Advanced Python after beginner python for intermediate learners
barmansneha1204
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
abhi353063
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
Daniele Pozzobon
 
Introduction to Python Values, Variables Data Types Chapter 2
Introduction to Python  Values, Variables Data Types Chapter 2Introduction to Python  Values, Variables Data Types Chapter 2
Introduction to Python Values, Variables Data Types Chapter 2
Raza Ul Mustafa
 
qb unit2 solve eem201.pdf
qb unit2 solve eem201.pdfqb unit2 solve eem201.pdf
qb unit2 solve eem201.pdf
Yashsharma304389
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
C++ Homework Help
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
Vipul Naik
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
Syntax directed definition and intermediate code generation
Syntax directed definition and intermediate code generationSyntax directed definition and intermediate code generation
Syntax directed definition and intermediate code generation
JananiRamannachetty1
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
appasami
 
Each n Every topic of C Programming.pptx
Each n Every topic of C Programming.pptxEach n Every topic of C Programming.pptx
Each n Every topic of C Programming.pptx
snnbarot
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
Rai University
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
Dmitri Nesteruk
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
Nitesh Dubey
 
advanced python for those who have beginner level experience with python
advanced python for those who have beginner level experience with pythonadvanced python for those who have beginner level experience with python
advanced python for those who have beginner level experience with python
barmansneha1204
 
Advanced Python after beginner python for intermediate learners
Advanced Python after beginner python for intermediate learnersAdvanced Python after beginner python for intermediate learners
Advanced Python after beginner python for intermediate learners
barmansneha1204
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
abhi353063
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
Daniele Pozzobon
 
Introduction to Python Values, Variables Data Types Chapter 2
Introduction to Python  Values, Variables Data Types Chapter 2Introduction to Python  Values, Variables Data Types Chapter 2
Introduction to Python Values, Variables Data Types Chapter 2
Raza Ul Mustafa
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
C++ Homework Help
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
Vipul Naik
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
Syntax directed definition and intermediate code generation
Syntax directed definition and intermediate code generationSyntax directed definition and intermediate code generation
Syntax directed definition and intermediate code generation
JananiRamannachetty1
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
appasami
 
Each n Every topic of C Programming.pptx
Each n Every topic of C Programming.pptxEach n Every topic of C Programming.pptx
Each n Every topic of C Programming.pptx
snnbarot
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
Rai University
 

More from mecklenburgstrelitzh (20)

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

Recently uploaded (20)

Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Ad

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.
  翻译: