Problem Set 1: An introduction to SML

Due Wednesday, September 5, 2007, 11:00 pm.


The goal of this problem set is to expose you to the basics of the SML language 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

This problem set has three parts. You will write your solution to each part in a separate file: part1.sml, part2.sml, part3.sml. To get you started, we have provided a stub file for each part. You can download and edit these files. Please write your name and NetID as a comment at the top of each file. In SML, comments are written (*like this*). Once you have finished, submit your solution using CMS.

Three important notes about grading:

  1. Compile errors: 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.
  2. Missing functions: We will be using an automatic grading script, so it is crucial that you name your functions and order their arguments according to the problem set instructions, and that you place the functions in the correct files. Crucial. Names and argument order. According to instructions. Crucial. Do not break this rule. You will be penalized. It would be tragic for you to not receive credit for a function properly written simply because our grading script couldn't find it.
  3. Code style: Finally, please pay attention to style. Refer to the CS 312 style guide and lecture notes. Programs that compile and execute correctly WILL NOT RECEIVE FULL CREDIT if they are poorly written/unstylistic. Take the extra time to think out the problems and find the most elegant solutions before coding them up. Even though only the first part of the assignment explicitly addresses the style issue, good programming style is also required for the other parts of this assignment, and for all the subsequent assignments in the rest of the semester. 

 

Part 1: Improving code style

[ Download stub file: part1.sml ]
The functions below compile and execute correctly. However, they are poorly written. Your task is to rewrite each of the function in an elegant, stylistic, and efficient manner, without changing their functionality. Take the time to carefully read the SML style guide before you write your solution.

fun compute(n:real*real*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 logic(x:(bool*bool)): bool = if #1 x then if #2 x then false else true else false

fun mul(l: int list): int =
if null l then 1 else if (hd l) = 0 then 0 else (hd l) * mul(tl l)

 

Part 2: Simple Functions

[ Download stub file: part2.sml ]
  1. Write a function revint : int -> int that produces a number whose decimal digits are reversed. The sign of the number remains unchanged:
    revint 312 = 213
    revint ~312 = ~213
    revint 0 = 0
  2. Consider positive numbers in binary form written as sequences of 0's and 1's, starting with the most significant bit. For instance, [1, 1, 0] is number 6. The empty list represents number 0 and lists must have no leading zeroes. Write a function addbin : int list * int list -> int list that adds two numbers written in this form.

 

Part 3: Text Processing

[ Download stub file: part3.sml ]
This part asks you to write functions that process strings. One particularly elegant way to code string-processing functions involves lists. We suggest that you look at the text-related functions in the STRING and CHAR modules modules of the Basis Library. For instance, you may want to use the explode function in STRING; this function  can be called using the syntax String.explode(args).
  1. Write a function words : string -> string list that takes a string and returns the list of words in the original string, in the order the appeared in the original string. A word is a sequence of alpha-numeric characters.
     
  2. Write a function emphasize : string -> string that converts a string into a similar string where all of the text between underscores is turned into upper-case letters, and all of the remaining text is left unchanged. The underscores are removed in the process. 
    emphasize "CS312 is _fun_" = "CS312 is FUN"