Problem Set 1 An introduction to SML
Due: Tuesday, September 10th, 4:00 PM
Instructions: you will do this problem set by modifying the source file ps1.sml
and submitting the program that results. The program that you submit must
compile. Programs that do not compile will receive an
automatic zero. If you are having trouble getting your
assigment to compile, please visit consulting hours. If you run out of time,
it is better to comment out the parts that do not compile and hand in a
file that compiles, rather than handing in a more complete file that does
not compile.
Note: for functions that are not implemented, you should see something of
the form "raise Fail "Not implemented."". You
should remove this text and replace it with the body of the function.
There are four separate parts to this assignment.
Update 9/5
The GCD definition has been updated to include the following rules:
GCD(0, m) = m
GCD(m, n) = GCD(n, m)
You may also assume that all inputs to GCD are valid (greater than or equal
to zero).
Update 9/4
The conversions for problem 3d are defined as follows:
1 liter = 1.0566882 quarts
1 quart = 0.94635295 liters
1 quart = 4.00000000 cups
You can figure out the rest of the conversion rates from these numbers.
Part 1: Posting to the newsgroup
Post a test message to cornell.test with the subject "CS312
Test".
DO NOT post test messages to the course newsgroup or you
may be penalized.
Part 2: Improving Style
The following functions are all technically correct, but very difficult to
read. Fix the indentation, use let to avoid unnecessary expressions, or
whatever else you feel is needed to fix any violations of the style
guide.
-
(* This function takes in two parameters, a string representing the base word that needs to have dashes appended to it, and an integer representing the number of dashes to append to the base string. The function recursively calls itself, appending a dash to the base each time until the count variable is less than 0, at which point it appends a final star. The final result is the string produced by appending an appropriate number of dashes and a star to the base string. *)
fun foo (mommy, rudolf) =
if (mommy <= 0) then rudolf^"*"
else foo(mommy-1, rudolf^"-")
-
fun partB (x:(bool*bool)) =
if #1(x) then if #2(x) then true else false else true
-
fun fib (x) =
case x
of 0 =>
1 | 1 => 1 |
_ =>
fib(x-1) + fib(x-2)
-
fun printFibs(x) =
print ("The Fibonacci number at position " ^ Int.toString x ^ " is "
^ Int.toString(fib(x)) ^ ".\n" ^ Int.toString(fib(x))
^ " is my favorite number. Is " ^ Int.toString(fib(x))
^ " your favorite number too?\n")
Part 3: Value and Function Declarations
Write val and fun declarations as follows:
- Assign a tuple with the names of all the people on course staff to the
variable reallyCoolPeople.
- Bind the last letter on the "Installing Course Software" page to
the variable lastLetter of type char.
- Bind your name, age and whether you intend to be a CS major to a
record, personal of type {name:string, age:int,
cs:bool}.
- Write the function cookingConversions which takes a string
(either "CUPS", "QUARTS", or "LITERS") representing the units to
convert from, a real representing the number of units, and another
string (either "CUPS", "QUARTS", or "LITERS"), representing the units
to convert to. Assume that only those three strings will be passed
into the function (that is, you do not have to check for bad inputs). The
function should return the number of units in the converted
measure.
-
Declare a function gcd with the type int * int -> int for computing the
Greatest Common Divisor based on the following equations: (from Paulson and
Brandon)
GCD(0, m) = m
GCD(2m, 2n) = 2 x GCD(m, n)
GCD(2m, 2n + 1) = GCD(m, 2n + 1)
GCD(2m + 1, 2n + 1) = GCD(n - m, 2m + 1) m < n
GCD(m, m) = m
GCD(m, n) = GCD(n, m)
Part 4: Using the Basis Library
The basis library is one of the most helpful resources you have on
your quest to learn SML. For this part of the assignment, you should
browse The
Standard ML Basis Library and then write short (one line)
functions using the functions in the basis library.
- Write a function, isFifthCharUpper, that takes a string as input
and returns true if the 5th character is Upper Case. It returns false if the
string is less than 5 characters long.
- Write a function, extractIntAndFraction, that takes a real and
returns a tuple of an int and a real, where the int is the integer part of the
number and the real is the fraction part of the number.
- Write a function, currentTimeString, that gets the current time
and returns a string representing the local time in the format: "
DayOfWeek Month Day HH:MM:SS Year ". For example, this example would be
valid: "Fri Sep 13 20:00:00 2002"
Prepared by Justin Koser and Jackie Bodine for CS312
Fall 2002.