The goal of this problem set is to expose you to as much of the SML language as possible while learning a bit about functional style programming. If you are not already familiar with SML, it may be a fair amount of new material, so please begin this problem set early.
Instructions: You will do this problem set by modifying the files found in ps1.zip and submitting these through CMS. All programs that you submit must compile. Programs that do not compile will receive an automatic zero. If you are having trouble getting your assignment 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. Also, please pay attention to style. Refer to the CS 312 style guide and lecture notes.
For each of the problems, you will modify an included source file. 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 some of your code using an automated test script. Do not change published variable names, function names and types. Do not remove our delimiters. Pay careful attention to types.
There are six 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.
A. Provide the types of each of the following expressions, by modifying the included source file types.txt.
"hello"
[ (2.5, #"h"), (3.1415, #"d"), (42.2, #"z") ]
(fn y:int => "312!", "312", (312.01, 312), 213)
fn x => (fn y => (x - 1) > (y + 42))
(fn (x, y) => case y of 0 => 1 | 312 => 2 | _ => 3) (92, 29)
fn (a, c, d) => case c of [] => a andalso (d = "hi") | 312::xs => a | _ => (d = "bye")
let val a = 52 val b = a::[29, 30, 31] in (b, a) end
B. Give expressions which have the following types:
int * bool -> int
char list * (bool -> int) * (int list list)
int -> bool -> int
(char * (string list)) list
(bool * int -> string) * bool list
These are defined in the included file expressions.sig signature. Implement them by modifying the Expression structure found in file expressions.sml.
The following functions can be found in the included file style.sml. Find parts which violate the style guide and fix them. At the top of each function add comments briefly describing the style problems you have identified. Do not change function names or function types.
fun funkyLogic(a:bool, b:bool):bool = if a = true then (if not (b = false) then true else false) else if not b then false else true
fun addList(l:int list):int = if null(l) then 0 else (hd l) + addList(tl l)
fun anotherFunction(x:string, y:int):int = case x of "hello" => 5 | "bye" => 4 | "harlan" => (case y of 1 => 23 | 0 => 99 - y | _ => 365) | "cs312" => 42 + y | _ => ~20 * y
fun yetAnotherFunction(a:int*int*int, b:int):int = ((#2 a) * b) + (#1 a) - ((#3 a) * (#2 a))
Write the following val declarations by modifying the included file valDecl.sml. Do not change variable names or types.
Write the following fun declarations by modifying the included file funDecl.sml. Do not change function names or function types.
fun fact(x:int):int = raise Fail "Not implemented"
datatype position = FRESHMAN | SOPHOMORE | JUNIOR | SENIOR | MS of real | PHD of int fun hourlyRate(x:position):int = raise Fail "Not implemented"The real associated with a MS is that person's GPA. The int associated with a PHD is the number of years they have been a PHD student.
fun multiplyList(l:int list):int = raise Fail "Not implemented"
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. Modify the included file basislibrary.sml.
fun removeSpaces(s:string):string = raise Fail "Not implemented"
fun sumSizes(s1:string, s2:string):int = raise Fail "Not implemented"
fun flipCase(s:string):string = raise Fail "Not implemented"