$29
Change Log
1.1 Changed problem 30 from sign to sign times, and correct the type information in the add a problem, Problem 14.
1.0 Initial Release.
Objectives and Background
The purpose of this ML is to test the student’s ability to
start up and interact with OCaml;
define a function;
write code that conforms to the type specified (this includes understanding simple Ocaml types, including func-tional ones);
Another purpose of MPs and MLs in general is to provide a framework to study for exams. Several of the questions on each exam will appear similar to the MP and ML problems. By the time of the exam, your goal is to be able to solve any of the following problems with pen and paper in less than 2 minutes.
Done in Computer-Based Testing Facility
You are asked to sign up for a time next week to go to the CBTF, where you will be given a random portion (less than 25%) of this assignment to complete for your grade for the assignment. We recommend that you do the whole assignment beforehand to be comfortable with the problems, so you will be able to do the problems efficiently when you go in. To help yourself in that, please read the Guide for Doing MPs at
http://courses.engr.illinois.edu/cs421/fa2017/cs421D/mps/index.html
Be aware that if we have difficulties with the CBTF, this assignment will revert to an MP and will be turned in, in full, in the same manner as MPs.
Problems
Note: In the problems below, you do not have to begin your definitions in a manner identical to the sample code, which is present solely for guiding you better. However, you have to use the indicated name for your functions and values, and they will have to conform to any type information supplied, and have to yield the same results as any sample executions given, as well as satisfying the specification given in English. You need to leave the open Common directive at the top of the file. If you remove it, your assignment may be graded incorrectly, costing you points.
1
4.1 Variable Declaration
(1 pt) Declare a variable title with the value "ML 1 -- Basic OCaml". It should have type string. It should not contain a “newline”.
(1 pt) Declare a variable greetings with the value "Hi there.". It should have type string. It should not contain a “newline”.
(1 pt) Declare a variable address with a value of "Greetings, my friend!". It should have the type of string. It should not contain a “newline”.
(1 pt) Declare a variable frozen with a value of "Do you want to build a snowman?". It should have the type of string. It should not contain a “newline”.
(1 pt) Declare a variable daffy with a value of "Th, th, that’s all, Folks!". It should have the type of string. It should not contain a “newline”.
(1 pt) Declare a variable a with the value 17.5. It should have type int.
(1 pt) Declare a variable pi with a value of 3.14159. It should have the type of float.
(1 pt) Declare a variable e with a value of 2.71828. It should have the type of float.
(1 pt) Declare a variable quarter with a value of 0.25. It should have the type of float.
(1 pt) Declare a variable x with the value 32.7. It should have type float.
4.2 Simple Function Declaration
11. (2 pts) Write a function myFirstFun that returns the result of multiplying the sum of a given integer and 3 by 4.
# let myFirstFun n = ... ;;
val myFirstFun : int - int = <fun
myFirstFun 17;; - : int = 80
(2 pts) Write a function firstFun that returns the result of multiplying a given integer by 2 and adding 5.
let firstFun n = ... ;;
val firstFun : int - int = <fun
firstFun 12;; - : int = 29
(2 pts) Write a function square that returns the result of multiplying a given integer by itself.
2
# let square n = ... ;;
val square : int - int = <fun
square 7;; - : int = 49
(2 pts) Write a function times 13 that returns the result of multiplying a given integer by 13.
let times_13 n = ... ;;
val times_13 : int - int = <fun
times_13 7;; - : int = 91
(2 pts) Write a function cube that returns the result of multiplying a given integer by itself twice (i.e. the cube of the input).
let cube n = ... ;;
val cube : int - int = <fun
# cube 5;;
- : int = 125
4.3 Variable Use
16. (2 pts) Write a function add a that adds the value of a from Problem 6 to its argument.
# let add_a n = ... ;;
val add_a : float - float = <fun
add_a 13.0;;
- : float = 30.5
(2 pts) Write a function circumference that, when given a radius as a float, returns the circumference of a circle of that radius. You should use the value given in Problem 7 for .
let circumference r = ...;;
val circumference : float - float = <fun
circumference 1.0;; - : float = 6.28318
(Your value may vary slightly from that printed here if you use a machine of different precision.)
(2 pts) Write a function divide e by that returns the result of dividing the value you gave in Problem 8 by the given float. You will not be tested on the value 0.0.
# let divide_e_by x = ...;;
val divide_e_by : float - float = <fun
divide_e_by e;; - : float = 1.
(Your value may vary slightly from that printed here if you use a machine of different precision.)
(2 pts) Write a function plus quarter times 3 y that returns the result of multiplying by the float 3.0 the result of adding the value of quarter from Problem 9 to the float-valued input.
3
# let plus_quarter_times_3 y = ... ;;
val plus_quarter_times_3 : float - float = <fun
plus_quarter_times_3 23.5;; - : float = 71.25
(Your value may vary slightly from that printed here if you use a machine of different precision.)
(2 pts) Write a function square plus x y that returns the result of adding the value of x from Problem 10 to the square of the float-valued input.
# let square_plus_x y = ... ;;
val square_plus_x : float - float = <fun
# square_plus_x 23.17;;
- : float = 569.548900000000117
(Your value may vary slightly from that printed here if you use a machine of different precision.)
4.4 Conditional Expressions
(3 pts) Write a function salutations that takes a string, which is assumed to be a person’s name, and prints out a greeting as follows: If the name is "Elsa", it prints out the string
Halt! Who goes there!
followed by a “newline” at the end (that is, there should be only one “newline” produced). For any other string, it first prints out "Hail, ", followed by the given name, followed by ". We warmly welcome you!", followed by a “newline”. Do not print the quotations; they were included to help make blank spaces visible. All spaces in the sample text above and below are one space long.
# let salutations name = ... ;;
val salutations : string - unit = <fun salutations "Malisa";;
Hail, Malisa. We warmly welcome you!
- : unit = ()
(3 pts) Write a function hail that takes a string, which is assumed to be a person’s name, and prints out a greeting as follows: If the name is "Elsa", it prints out the string
Wayell, hah theya, Ayelsa!
(no “newline” at the end). For any other string, it first prints out "Dear, ", followed by the given name, fol-lowed by ". I wish you the best in CS421.", followed by a “newline”. Do not print the quotation marks. All spaces in the sample text above and below are one space long.
# let hail name = ... ;
val hail : string - unit = <fun
# hail "Thomas";;
Dear, Thomas. I wish you the best in CS421.
- : unit = ()
(3 pts) Write a function welcome that takes a string, which is assumed to be a person’s name, and prints out a message as follows: If the name is "Elsa", it prints out the string
4
Can you come out to play?
(with a “newline” at the end). For any other string, it first prints out "Aw, come on, ", followed by the given name, followed by ". We’re going to have a wonderful time!", also followed by a “newline”. Do not print the quotation marks. All spaces in the sample text above and below are one space long.
# let welcome name = ... ;;
val welcome : string - unit = <fun
# welcome "John";;
Aw, come on, John. We’re going to have a wonderful time!
- : unit = ()
(3 pts) Write a function greet that takes a string, which is assumed to be a person’s name, and prints out a greeting as follows: If the name is "Elsa", it prints out the string
Hey Elsa, cool man!
(no “newline” at the end), and for any other string, it first prints out "Hello, ", followed by the given name, followed by: ". I hope you enjoy CS421." , followed by a “newline”. Do not print the quotation marks. All spaces in the sample text above and below are one space long.
# let greet name = ... ;;
val greet : string - unit = <fun
# greet "Angela";;
Hello, Angela. I hope you enjoy CS421.
- : unit = ()
(3 pts) Write a function salute that takes a string, which is assumed to be a person’s name, and prints out a greeting as follows: If the name is "Elsa", it prints out the string
What’s the low-down, man?
(no “newline” at the end). For any other string, it first prints out "Hey, ", followed by the given name, followed by "! Give me five, man.", not followed by a “newline”. Do not print the quotation marks. All spaces in the sample text above and below are one space long.
# let salute name = ... ;
val salute : string - unit = <fun
# salute "Ali";;
Hey, Ali! Give me five, man.- : unit = ()
(3 pts) Write a function rectangle area that takes two arguments of type float representing the length and width of a rectangle, and returns the area of that rectangle, if both inputs are greater than or equal to zero. If either input is strictly less than zero, return (-1.0) instead. Pay careful attention to the type of this problem.
# let rectangle_area l w = ... ;;
val rectangle_area : float - float - float = <fun
rectangle_area 25.3 19.2;; - : float = 485.76
5
(3 pts) Write a function diff square 9 that takes two floats, one and then another, and if the first float is strictly smaller than the second, then it returns the result of subtracting nine from the square of the second. If the first float is not strictly smaller than the second, and half the first is strictly larger than the second, return result of subtracting nine from the square of the first. If the first float is not strictly smaller than the second, and half the first is not strictly larger than the second, then return result of subtracting nine from the square of the difference of the first and the second.
# let diff_square_9 m n = ...;;
val diff_square_9 : float - float - float = <fun
diff_square_9 5.5 (-17.2);; - : float = 21.25
(3 pts) Write a function make bigger that takes a float and then another float and if the first input is strictly larger than 0.0, it adds the first to the second. If the first input is not strictly larger than 0.0 and the second is strictly less than 1.0, it returns the result of adding 1.0 to the second, and otherwise it returns the square of the second.
let make_bigger x y= ...
val make_bigger : float - float - float = <fun
make_bigger (-5.2) 12.0;; - : float = 144.
(3 pts) Write a function has smallest square that, when given one integer and then another, returns the one that has the smallest squared value. If they have the same squared value, but are not the same value, it should return the smallest value given.
let has_smallest_square m n = ... ;;
val has_smallest_square : int - int - int = <fun
has_smallest_square 4 6;; - : int = 4
(3 pts) Write a function sign times that, when given a first and second integer, returns 1 if the product of the integers is positive, 0 if the product of the integers is zero and -1 if the product of the integers is negative.
let sign_times n m = ... ;;
val sign_times : int - int - int = <fun
sign_times 4 3;; - : int = 1
6