Problem Set 1 An introduction to SML

Due Tuesday, February 3, 11:59pm


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.

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: Expressions and Types

Provide the types of each of the following expressions, by modifying the included source file types.txt

  1. [(1, "100"), (2, "211"), (3, "312")]
    
  2. fn x =>
      (case x of
        (0, y) => y + 1
       |(y, 0) => y - 1
       |(y1, y2) => y1 + y2)
    
  3. if ("3" ^ "1"  ^ "2") = "312" then ["3", "1", "2"] else nil
    
  4. let
      val x::xs = ["welcome", "to", "312"]
    in
      (x, xs)
    end
    
  5. (fn (arg1, arg2) => (arg2, arg1))   (hd([3, 1, 2]), "hello")
    
  6. fn (x, y, z) => ((x = "hello") andalso (y < 3)) orelse z

Part 3: Improving Style

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.

  1. 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))
    
  2. 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")
    
  3. 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)
    
  4. 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
    
    

Part 4: Value Declarations

Write val declarations as follows:

  1. Please bind the variable someLetter to the fourth letter of this sentence and the variable someWord to the fourth word of this sentence. (HINT: The first letter is "P", not "a" or anything silly like that.)


  2. Bind SectionLeader1 to a record of type {name:string, age:int, favoriteFood:string}. You may wish to obtain this information in section, as we will not post it to the newsgroup or give it out on the email list. Please do the same for SectionLeader2. SectionLeader1 and SectionLeader2 must be distinct.

Part 5: Function declarations

Consider the following classification of course staff members.

     1) professor
     2) section TA
     3) consultant
     4) not a staff member
  1. Find out the name and position of all CS 312 staff members and implement function getPosition. Given the first name (all letters in lower case) of a staff member of this course, this function should return the number representing the position held by the given member. (If the given name is not the name of a staff member, the returned number should be 4)
         fun getPosition (name:string) : int = raise Fail "not implemented"
    
     Ex: getPosition ("andrew") = 1
    
  2. The department has decided to give an extra payment for all staff members. For each hour of work, each consultant will receive 2 bananas, TAs will receive 3 bananas, and professors will receive 5 bananas (people who are not staff members receive no bananas). Use getPosition to implement function getNumberBananas, which returns the number of bananas given to a specific CS 312 staff member.
         fun getNumberBananas (name:string) : int = raise Fail "not implemented"
    
     Ex: getNumberBananas ("maya") = 3
    
  3. Finally, use the previously defined function getNumberBananas to implement function getsMoreBananas. Given two names, getsMoreBananas returns the name of the person who receives a higher amount of bananas per hour from the department (In case the number of bananas for both are the same, return the name which comes first in alphabetical order).
         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 **

Part 6: 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 functions using the functions in the basis library.

  1. Implement function circleArea that returns the area of a circle with the given radius. In case the input is negative, convert it to a positive value before calculating the area.
         fun circleArea (radius:real) : real = raise Fail "not implemented"
    
     Ex: circleArea (3.0) = circleArea(~3.0) = 28.2743338823
    
  2. Implement a function stringToInt that converts a string into an integer value. You should not use the library function "Int.fromString" in this exercise. You may consider that the given string will represent a valid nonnegative integer value.
         fun stringToInt (numString:string) : int = raise Fail "not implemented"
    
     Ex: stringToInt ("312") = 312