Problem Set 1 An introduction to SML

Due Tuesday, January 28, 11:59pm


Instructions: you will do this problem set by modifying the source file ps1.sml (which can be found in ps1.zip) 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.

We will test your code using an automated test script. Do not change variable names. Do not change function names. Do not remove our delimiters. 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: Improving Style

The following code is despondent about its appearence. 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.

  1. fun max3(x:int*int*int) =
    let val r = Int.max(#1 x, #2 x) in Int.max(r, #3 x) end

  2. fun abc(x:bool, y:bool, z:bool) : bool =
    if x then if y then true else
    if (x andalso z) then true else false
    else false

  3. fun t(b:bool, x:real) : real =
    case b of false => Math.cos(x)*Math.cos(x)
    | true => (Math.sin(x)+1.0)*(Math.sin(x)+1.0)

  4. fun danQuayle(X1:real*real*real,X2:real*real*real):real*real*real =
    let
    val a = #1 X1
    val b = #2
    X1
    val c = #3 X1
    val d = #1 X2
    val e = #2 X2
    val f = #3 X2
    in
    ((((((b*f) - (c*e)), ((c*
    d) - (a*f)
    ), ((a*e)-(b*d))))))
    endi

Part 3: Value and Function Declarations

Write val and fun declarations as follows:

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

  2. Bind SectionLeader1 to 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.

  3. Write a function, isGorges which takes a string and returns true if the string is "cs312", "recursion", or "lambda". Otherwise isGorges returns false. No credit will be awarded to solutions using if then else.

Part 4: Intermission

The following functions will not compile. Please correct this without deleting or commenting out any code.

  1. The corrected function should return the integer 3 when applied to unit. That is, f() evaluates to 3.

    fun f():int =
      let
        val x = "Toast is good."
      in
        x
      end
  2. This function should implement the following mapping:
         (#"l",_)=>"lambda"
         (#"r",1)=>"recursion is #1 in my book!"
         (#"r",2)=>"recursion is second best only to parametric polymorphism!"
         (#"r",_)=>"I've got nothing"
         (#"L",_)=>"Lambda"
         (_,_)   =>"unexpected character"
    Please note that the code you have been given looks ok, but does not follow cs312 style. This is intentional. While the optimal way to correct g would involve rewriting the case into proper style, do not do that here.
    fun g(s:char, t:int):string =
      case s of
        #"l" => "lambda"
      | #"r" =>  case t of
                   1 => "recursion is #1 in my book!"
                 | 2 => "recursion is the second best " ^ 
                        "only to parametic polymorphism!"
                 | _ => "i've got nothing"
      | #"L" => "Lambda"
      | _ => "unexpected character"

Part 5: 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 (preferably one line) functions using the functions in the basis library.

  1. Write toUpper: string->string. This function should defined such that toUpper(s) returns s with each lower case letter replace by the coresponding uppercase character.


  2. Write a function myArcSin. Given input x, myArcSin returns SOME(y)where sin y = x or NONE if x < -1.0 or x > 1.0.




Prepared by Vladimir Kolmogorov and Jeff Vaughan for CS312 Fall 2002.