Instructions: You will do this problem set by modifying the file types.txt (for part 2) and the source file ps1.sml (for the other parts), and submitting the modified files. These files can be found in ps1.zip. The program that you submit (ps1.sml) 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.
We will test your code using an automated test script. Do not change variable names. Do not change function names. Do not remove our delimiter comments. Pay scrupulous attention to types. You have been warned.
There are five separate parts to this assignment.
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.
Provide the types of each of the following expressions, by modifying the included
source file types.txt
[(1, "100"), (2, "211"), (3, "312")]
fn x =>
(case x of
(0, y) => y + 1
|(y, 0) => y - 1
|(y1, y2) => y1 + y2)
if ("3" ^ "1" ^ "2") = "312" then ["3", "1", "2"] else nil
let val x::xs = ["welcome", "to", "312"] in (x, xs) end
(fn (arg1, arg2) => (arg2, arg1)) (hd([3, 1, 2]), "hello")
fn (x, y, z) => ((x = "hello") andalso (y < 3)) orelse z
The following code is despondent about its appearance. Give it a makeover! While each function is technically correct, they are all 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 may include replacing poorly written code with equivalent but stylish code.
fun rt (n:real*real*real)= ((~1.0*(#2 n)+Math.sqrt((#2 n)*(#2 n)-4.0*(#1 n)*(#3 n)))/2.0*(#1 n), (~1.0*(#2 n)-Math.sqrt((#2 n)*(#2 n)-4.0*(#1 n)*(#3 n)))/2.0*(#1 n))
fun whatsMyMood (action:string, course:int):string =
(case action of
"doing homework" => (case course of
001 => "bored"
| 312 => "thrilled"
| _ => "unknown")
| "leaving class" => (case course of
001 => "happy"
| 312 => "sad"
| _ => "unknown")
| _ => "unknown")
fun boolFun (a,b,c) =
if a=true then
(case (b,c) of
(true,true) => true orelse a
| (true,false) => a orelse b
| (false,true) => a orelse not b
| (false,false) => true)
else (case (b,c) of
(true,true) => a
| (true,false) => b
| (false,true) => not c
| (false,false) => true)
fun mathFun (x:int, y:int) = let fun helper(a1:int, a2:int)= case (a1, a2) of (0, _) => (a2, 0) | (_, 0) => (0, a1) | _ => (a2, a1) fun operation(a1:int, a2:int) = if (a1 < a2) then ~(a2 - a1) else (a1 - a2) in operation(helper(x, y)) end
Write val declarations as follows:
Consider the following classification of course staff members.
1) professor
2) section TA
3) consultant
4) not a staff member
fun getPosition (name:string) : int = raise Fail "not implemented"
Ex: getPosition ("andrew") = 1
fun getNumberBananas (name:string) : int = raise Fail "not implemented"
Ex: getNumberBananas ("maya") = 3
fun getsMoreBananas (name1:string, name2:string): string = raise Fail "not implemented"
Ex: getsMoreBananas ("alex", "jeff") = "jeff"
** Alex is a consultant and Jeff a TA, so Jeff gets more bananas than Alex **
getsMoreBananas ("christina", "britney") = "britney"
** Neither is in the course staff, so both of them get 0 bananas - therefore, the
alphabetical order is used **
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 functions using the functions in the basis library.
fun circleArea (radius:real) : real = raise Fail "not implemented"
Ex: circleArea (3.0) = circleArea(~3.0) = 28.2743338823
fun stringToInt (numString:string) : int = raise Fail "not implemented"
Ex: stringToInt ("312") = 312