CS330 Spring 2004 Lecture 4

George Tzanetakis

Slides 

This lecture will not contain any slides as it consists mainly of
programming examples in sml.

SML code

Code in text format

Supporting web resources 

A gentle introduction to ML (Andrew Cumming)

Bob Harper's Programming in SML


Instructions to use sml

We will be using the SMLNJ implementation of SML. For instructions
see the link to the previous lecture.

Suggested exercises

5.1) A time of day can be represented as a triple (hours, minute, f) where f is either AM or PM, or
as a record. Declare an SML function to test whether one time of day comes before another. For
example, (11,59,"AM") comes before (1,15,"PM"). Make solutions with triples as well as with
records. Declare the functions in infix notation and use patterns to build readable declarations.

5.2) The set of complex numbers is the set of pairs of real numbers. Complex numbers behave
almost like real numbers if addition and multiplication are defined by
(a,b) + (c,d) = (a+c, b+d)
(a,b)  * (c,d) = (ac -bd, bc + ad)

a) Declare infix SML functions ++ and ** for addition and multiplication of complex numbers
b) The inverse of (a,b) wrt addition i.e -(a,b) is (-a, -b) and the inverse of (a,b) wrt to
multiplication i.e 1/(a,b), is (a / (a*a + b*b), -b/(a*a + b*b)) provided a and b are both not zero.
Declare infix SML functions of subtraction and division of complex numbers.
c) Use let-expressions in the declaration of the division of complex numbers in order to avoid
repeated evaluation of identical sub-expressions.
d) Declare a function to give a string representation of a complex number in standard
mathematical notation i.e (a,b) is written as "(a + bj)" where j is the sq.root of -1.