[SOLVED] 代写 data structure algorithm Scheme software Scheme Coding

30 $

File Name: 代写_data_structure_algorithm_Scheme_software_Scheme_Coding.zip
File Size: 574.62 KB

SKU: 6106184727 Category: Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Or Upload Your Assignment Here:


Scheme Coding
Added parameter f to heapremove . 930
Added simplification rule 18 and fixed a bug in the last output example. Removed point 7 under Submission as it is not relevant. Corrected list heap examples. Resigned as software developer because of such terrible mistakes. 103
Updated the last simplify example. It swapped the order of one subexpressions operands when it shouldnt have. 104
Heaps in Scheme Introductory Overview
Heaps support two primary operations: insertion into the heap and removing the highest priority item from the heap. For this assignment, you must write a leftistheap data type in scheme. Since scheme is not objectoriented you will simply write a set of functions that define operations on leftist heaps.
We define a leftistheap as a binary tree note that this does not suggest that heaps are binarysearch trees. A leftistheap also maintains the following two constraints
For every node N in the tree, the value of N has priority greaterthanorequalto that of any child. The meaning of highest priority in our assignment is very flexible. It is often defined, however, as smallest
Adding and removing are both implemented in terms of heapmerging.
Our implementation will define a leftistheap as the data structure described below.
1. The empty list is the empty heap.
2. Otherwise, a heap is a list of exactly three elements. The first element is the root value, the second element is the leftsubheap and
the third element is the rightsubheap.
Figure 1 shows a leftist heap where highestpriority is understood to mean smallest. The corresponding scheme representation is given in Figure 2.
Figure 1. Leftist Heap
Figure 2. Scheme represetation of the heap in Figure 1.
Functions to Write 35 Points
You must write the following heap functions. Each function must have a reasonably efficient runtime performance.
1. heapcreate Returns an empty heap.
2. heapisempty? h Returns t if heap h is empty and f otherwise.
3. heapsize h Returns the size of heap h i.e. the number of values in the heap
3 6
7 8 18 17 2637
12 18 24 33
10 21 14 23
4. heapmerge h1 h2 f Returns the heap that results from merging heap h1 with heap h2. Function f is a predicate that accepts two
CS 421 : Programming LanguagesKenny Hunt
elements of the type contained in the heap and returns t if the left operand is understood to have higher priority than the right.

5.
6. 7.
8.
9. 10. 11.
heapinsert h x f Returns the heap that results from inserting value x into heap h. Function f is a predicate that accepts two elements of the type contained in the heap and returns t if the left operand is understood to have higher priority than the right.
heappeek h Returns the highestpriority value in heap h.
heapcontains? h x heq : Returns t if heap h contains element x as defined by the heq predicate and false otherwise. Function heq is a predicate that accepts two elements of the type contained in the heap and returns t if the two elements are equal and f otherwise.
heapremove h f : Returns a heap that corresponds to heap h after removing its highest priority value. Function f is a predicate that accepts two elements of the type contained in the heap and returns t if the left operand is understood to have higher priority than the right.
heapnumberbefore h x f : Returns the number of items in heap h having a higher priority than x as defined by f where f is the function described in heapinsert.
listheap xs f Returns the heap that results from adding each element in list xs to an empty heap in the order they occur in xs using f as the heapinsert predicate. You must not use reverse . You may find folding helpful.
heaplist h f Returns a list of the elements of h in the order that they would be removed from h according to predicate function f. Your code must repeatedly call heapremove in order to generate the resulting list.
Algorithmic Notes
The fundamental operation for us is heapmerge, a way of taking two leftist heaps and merging them together to form a single leftist heap. You must use the following algorithm for merging two heaps
To insert an element X into a heap H, we first create a heap of size 1 containing X and then merge that heap with H. To remove H, we return the result of merging the leftsubtree of H with the right subtree of H.
Examples
Simple Expressions Description
A scheme expression is, in a sense, a preparsed abstract syntax tree. In this problem you will write a function that accepts a scheme expression and simplifies it into canonical form such that a more computationally efficient abstract syntax tree is produced. For this assignment, scheme expressions are defined in Figure 1.
merge h1, h2, f
let P be h1 if the root of h1 is of higher priority according to f than the root of h2.Otherwise P
let Q be h2 if P is h1.Otherwise Q is h1.
let RRIGHT be the leftsubtree of P
let RLEFT be the result of merging the rightsubtree of P with Q
return a heap having as its root the root of P with leftsubtree RLEFT and rightsubtree RRIGHT
listheap 3 1 5 9 8 2 7 4 6 1 5 6 87 2 4 3 9
listheap 3 1 5 9 8 2 7 4 6 9 8 6 7 5 4 3 1 2
listheap d e q w b r t lambda x y string? symbolstring x symbolstring yb d q t
e wr
listheap 1 3 5 12 0 5 a t t t t lambda x ylength x length ya t t t
t 1 3 5 12 0 5
1. A variable is a scheme expression.
2. The symbol error is a scheme expression.
3. A number is a scheme expression.
4. Assuming that E1, E2 and E3 are scheme expressions, then so are.
a.E1 E2
b.E1 E2
c.E1 E2
d.E1 E2
e. pick E1 E2 E3
5. The meaning of the first four operators is identical with numeric addition, subtraction, multiplication and division as defined in the scheme language with the exception of the error value as noted below. The PICK expression produces E2 if E1 is zero; otherwise it produces E3 if E1 is not error; otherwise it produces error.
Figure 1. Scheme Expressions
Simplify 55 Points
Write a function named simplify that takes a scheme expression and fully simplifies that expression by applying the simplification rules below. The phrase ERROR is used to mean error.
1. X0XforanyX
CS 421 : Programming LanguagesKenny Hunt
2. 0XXforanyX

3. X00foranyX
4. 0X0foranyX
5. X1XforanyX
6. 1XXforanyX
7. X0XforanyX
8. X1XforanyX
9. X0ERRORforanyX
10.X X1 for any X other than ERROR and 0 and 1
11.X YZ if X and Y are numbers and Z is the result of applying arithmetic operator12.X YERROR if either X or Y is an ERROR for any arithmetic operator
13.X X0 for any X other than ERROR
14.0 X0 for any X other than 0 and 1
15. pick 0 E1 E2E1
16. pick ERROR E1 E2ERROR
17. pick X E1 E2E2 for any nonzero number X
18. pick X E1 E2E1 if E1 is equal to E2 and X is not ERROR
Additionally, the following rules must be applied.
1. For any simplified scheme expression involving either multiplication or addition
a. If the operands are both variables, they are ordered alphabetically.
b. If the operands include a variable and a value, the variable precedes the value.
c. If one operand is a subexpression and the other is either a variable or value, the variable or value precedes the subexpression.
d. If the operands are both subexpressions, the ordering of the subexpressions follows the ordering of their operand as given in:
, , , , pick.
2. A fully simplified expression will have no subexpression that can be simplified by application of one or more of the above rules.
Examples
simplify 3 5 y 1x 0 8x y
simplifyz53 0error
simplifyz a a z
simplify 1212
simplify 5 2 912
simplifyx a a x
simplify 3 aa 31
simplifya 3 a 3
simplify3 a a 3
simplify 1 a 3 31 a
simplify xx
simplify pick 0 1 21
simplify pick error 1 2error
simplify pick 3 1 22
simplify pick3 0 1 2error
simplify pick a b cc 0 2 c2 1c c0 1b1 c 2
pick a b c c2 c c cbc 1 2
simplifyc a0 c pick 3 3 aa a b 01 c pick b 3 0 c
ca aa a 0 pick b 3 0 c
simplify pickpick 3 0 b3 3pick a c b3 1pick b b 1 1 pick a
2 1 pick 3 c 0 pick pick 1 c a0 22 0 ac 3 3 c pick 0 c 0pick
3 2 a0 2a 1 pick a c b 2 pick a 02 c pick b 3 b 30 pick
a a3 1 2 pick pick 2 c 02 1 2 2 1a 2 c0 bpick c 0 0
c b2 3pick a ca 3 a 00 1pick a a 2 pick 0 3 a 2
0 0 2b 3 pick1 a0 c a b cerror
simplifya pickpick10 b pick 2 b ab pick 2 a apickc 2 b
a c a pick pick pick 0 b 0 02 a ca 2 2b 1a2 2 pick 2 2 3
pick a 0 abc apick a pickb b3 33 bpick b 3 b c a 1pick
2 1c a pick 2 2 c cb2 0 pick 1 3 2 c 2b 10
pick 2 1 bac b1 b bpick a b b a 0 c0 a 2 1 a a
pick 2 c3 3 3 a1 2pick c 0 ac 3pick 31 c1 2
pickb ca 2c 33 pick b 2 c pick c b b30 a b0b
a3 1c b bb3 0b 2 pick c 2 3c 2b 03 3
1 a3 a 1 b pick c 1 2 c 2 b a 3 cpick a b ba c
a 3 cpick pick 3 b 0 1pick 0 c ab bpick pick 3 3 1 3c b0
2pick 0 c1 a 1 13 1pick b 2 b1 a 0 2b a 1
3c 3a 0 b b 1 pickpick a pick b a a3 1 a pick pick 2 2 3 2 3
2 b a cc a pick20 0 1 1c c cpickc 2 pick b c
2c b pick 20 3 pick b b 1 a pick pick1 c3 2a 1c bc c
pick 2 0 23 1 pickb c ab c pickb 1 pick pick a b a 2ac a pick pick c
a b a ab apick c 2 00 3picka 0 a pick 3 c 20 c
3 21 c pick 3 c0 a pick 1 b 3 c c 0a bpick b a 2 pick 1 b
c2 pick 0 a 1 pickpick b 0 b 3 2 c2 1b b 23 bc
pick3 1b aa b pick 2 a a1b 0 cc c 0 3 pick a 2
a a pickpick a1 bb aa pickc 2 ba c pick pick b
02 a ca0 ba21pick a 0 aba c error0a
1 b bb abab c a c 2 ac 3 pick bbc 2 3pick
CS 421 : Programming LanguagesKenny Hunt

Footprint 10 Points
Write a function named footprint that takes a scheme expression and returns an approximate memory footprint. The footprint is determined by 1 counting the unique integer literals in the expression and 2 counting the unique variables in the simplified expression. The footprint is then given as 4 times the sum of these counts.
Hint
Simplification requires ordering symbols. I recommend writing the following function; a function that accepts two symbols and returns true if the first symbol is less than the second.
Optimization 25 Points
Required of 521 students. Optional extra credit for 421 students.
Overview
Once an expression has been simplified, it may still be possible to optimize evaluation of the expression. For example, consider the expression a 33 a . This expression simplifies to a 3a 3 . Notice, however, that while the expression has been simplified, evaluation is inefficient since the subexpressiona 3 is evaluated twice. We can optimize this expression by extracting the common sub expression, evaluating it once, and then refering to that result as required. If we optimize the simplified expression, we might obtain let g15488a 3g15488 g15488 .
Function
Write a function named optimize that takes a scheme expression, simplifies that expression, and then extracts all commonsubexpressions into a let form. Follow the process outlined below when writing your solution.
1. Find all commonsubexpressions within the expression EXP. Atoms do not count as common subexpressions.
2. Find the shortest of those common subexpressions. Call it SEXP.
3. Create a let V SEXP .. E expression such that SEXP is bound to V in the let and all occurrences of SEXP in EXP are replaced
define symbol? s1 s2 string? symbolstring s1 symbolstring s2
by V to obtain E.
4. Repeat this process until there are no more common subexpressions.
Examples
Submission
1. You must submit all your work using
using a project named cs421 with a folder named hw2. Here is a .
a a a a
a 11 alet g128572a 1g128572 g128572
picka 11 ab1 alet g128573a 1 pick g128573 g128573b g128573
ab 1 1 b alet g128574b 1 g128575a g128574g128575 g128575
GitLab
GitLab tutorial
2. The scheme code must be placed in two seperate files: one named heap.rkt and one named simplify.rkt. Of course, the heap.rkt file contains all of the functions related to the heap code and the simplify.rkt contains all functions related to the simplification simplify, footprint, and perhaps optimize . Each of these files must be contained in the hw2 folder.
3. You must have the statmement lang racket as the first line in each of your scheme files.
4. You must have a statement provide namesofrequiredfunctions as the last line in each of your scheme files. This line must
provide each function required by the homework assignment for that particular file.
5. The Scheme functions you submit must not use any imperative features. Do not use set, while, display, and begin! You will receive 0 points
for any problem that uses these constructs. You may only use define to define functions at the topmost level of the interpreter; any other use
will incur a loss of all points for that problem.
6. Do not write code that breaks a problem into more cases than necessary. Points will be deducted for including unnecessary cases.
CS 421 : Programming LanguagesKenny Hunt
62 c 3 a 2pick c 0 ac 32 pickb ca 2c 3b 3
b 2 pick c 2 3bb4b cbc 2a 13 ab pick c 1
2 c b 2 a c 3ba ca 3 error
lmth.lairotutlairotutbaltigderahsude.xalwu.sc.ytirahc:ptth 34491:ude.xalwu.sc:sptth

CS 421 : Programming LanguagesKenny Hunt

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] 代写 data structure algorithm Scheme software Scheme Coding
30 $