Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Three F# Exercises This assignment consists of three F# exercises. The code for all exercises are included in the single file, hw4.fsx. For each of

Three F# Exercises This assignment consists of three F# exercises. The code for all exercises are included in the single file, hw4.fsx. For each of the exercises, you are only required to write the provided functions. There is no additional global functionality. Your functions should produce no output. To test your functions, either: Test the functions using the interactive simulator Add your own tests to the script (but be sure to remove them or comment them out before submission) Implementation Rules You may define and use additional functions if your feel it is appropriate. Without prior approval from me, you may only use the subset of F# described in class. o In particular, you may NOT use F# system functions (such as min) or methods o Even though they were mentioned in class, you are NOT allowed to use the methods in the List module such as List.map First Exercise, maxcylindervolume Write a function maxCylinderVolume that takes a list of floating-point tuples that represent dimensions of a cylinder and returns the volume of the cylinder that has the largest volume. Each tuple has two floating point values that are both greater than zero. The first value is the radius r and the second value is the height h. The volume of the cylinder is computed using ar?h. The value n is represented in F# with System. Math.PI. (10,

If the list is empty, return 0.0. Examples: > maxCylindervolume [(2.1, 3.4); (4.7, 2.8); (0.9, 6.1); (3.2, 5.4)];; val it : float = 194.3137888 > maxCylinderVolume [(0.33, 0.66)];; val it : float = 0.2257988304 Second Exercise, elimDuplicates Write a function elimDuplicates that takes a list of integers and eliminates consecutive duplicates; replacing them with a single instance of the value. Order is preserved and non- consecutive duplicates are unaffected. Examples: = > elimDuplicates [1; 2; 2; 3; 3; 3; 4; 4; 4; 4; 5; 5; 5; 5; 5];; val it : int list [1; 2; 3; 4; 5] > elimDuplicates [1; 2; 2; 1; 3; 3; 1; 4; 4; 1; 5; 5; 1];; val it: int list [1; 2; 1; 3; 1; 4; 1; 5; 1] = Third Exercise, BST Write the following binary search tree functions for a binary search tree of integers. Use the following type definition for a BST (copy this into your solution): // Tree definition for problem 3 type BST = | Empty | TreeNode of int * BST BST A. insert value tree : Inserts the value into the tree and returns the resulting tree. The resulting tree does NOT need to be balanced. If the value already exists in the tree, return the tree without inserting the value. B. search value tree : Returns true if the value is in the tree and false otherwise. C. count func tree : The parameter func is a Boolean function that takes a single parameter and returns true or false. The function tests the value of each node with func and returns the number of nodes that evaluate to true. D. evenCount treeReturns the number of nodes that contain even integers. REQUIREMENT: This function must be a single call to count (part 3C) using a lambda function. Examples: > let btl = insert 10 Empty; ; val btl : BST TreeNode (10, Empty, Empty) > let bt2 = insert 5 bt1;; val bt2 : BST = TreeNode

TreeNode (5, Empty, Empty), Empty) > let bt3 = insert 3 bt2;; val bt3 : BST - TreeNode (10, TreeNode (5, TreeNode (3, Empty, Empty), Empty), Empty) > let bt4 = insert 17 bt3;; val bt4 : BST- TreeNode (10, TreeNode (5, TreeNode (3, Empty, Empty), Empty), TreeNode (17, Empty, Empty)) > let bt5 = insert 1 bt4;; val bt5 : BST = TreeNode (10, TreeNode (5, TreeNode (3, Empty, Empty), Empty), TreeNode (17, TreeNode (12, Empty, Empty), Empty)) > search 17 bt5;; val it : bool = true > search 4 bt5;; val it : bool - false > evenCount bt5;; val it : int - 2 Guidance Note: Wanted to clarify that your program will not be penalized if the compiler emits warnings and your program works. F# will warn you anytime a run-time error is possible and, due to limitations in its analysis, will emit warnings in cases where a run-time error is not possible. One situation occurs is when using this statement: let hd::tl - list You'll get a warning indicating that incomplete pattern matching occurs on this expression and mentions the [] (empty list) case. It is true that you will get a runtime error if this statement executes when list is empty. You should check your code to make sure it is handled. However, even if you guarantee that list is not empty, the compiler will still emit the warning. The only way to avoid the warning is to use pattern matching and extract hd and tl using one of the patterns.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Core Ios Developer S Cookbook Core Recipes For Programmers

Authors: Erica Sadun ,Rich Wardwell

5th Edition

0321948106, 978-0321948106

Students also viewed these Programming questions

Question

QQ Understand how the Web works.

Answered: 1 week ago

Question

QQ Explain the current structure of the Internet.

Answered: 1 week ago

Question

QQ Understand the impact of mobile applications.

Answered: 1 week ago