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.
[ 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 solve(a:real, b:real, c:real) : real*real = if b * b - 4.0 * a * c >= 0.0 andalso a > 0.0 then ((~b + Math.sqrt(b*b-4.0*a*c))/(2.0*a), (~b - Math.sqrt(b*b-4.0*a*c))/(2.0*a)) else (0.0,0.0) fun multiply(l:int list) : int = if (null(l)) then 1 else ((hd(l)) * (multiply(tl(l)))) fun add(n:int*int, m:int*int) : int*int = ((#1 n * #2 m) + (#2 n * #1 m), #2 n * # 2 m)
binary : int -> bool *
string that takes an integer and returns a pair of: a
boolean that indicates the sign; and a string showing the
binary representation of its absolute value. The result must
not have leading zeroes, except for number 0.
binary 0 = (true, "0") binary 6 = (true, "110") binary ~6 = (false, "110") binary 312 = (true, "100111000")
swap : int list -> int list that
takes a list of integers and swaps each pair of consecutive
numbers (i.e., it swaps the numbers at positions 2i and
2i+1). If the list has an odd number of elements, the last
element remains unchanged. For instance:
swap [1,2,3,4] = [2,1,4,3] swap [1,2,3,4,5] = [2,1,4,3,5]
explode
(and other functions) in the STRING structure in the
Basis Library. You can call such functions using the syntax
String.explode(args).
compact : string -> string that replaces
each sequence of one or more whitespace characters with a single blank
character. capitalize: string -> string that
capitalizes strings. Given a string, the function rewrites each word
in the string such that the first letter is upper-case, and all of the
others are lower-case. Words are sequences of alphanumeric characters
(digits or letters); all other characters separate distinct words.
capitalize "FUNctional programming is FUN" = "Functional Programming Is Fun" captialize "red-black trees" = "Red-Black Trees"