ML Lecture 5
The examples on the slides for this class are
here.
Choosing a Linux machine from an xterm -- pay attention to load average
(lower is better)
Understanding SML error messages:
-
In the error message is the filename, followed by a colon and a number
like "12.23". This number is the line (12) and the character on the line
(23) that the error was first detected. It may not be the actual error,
but that is where SML first detected that an error was occurring.
Labs - Grading issues
-
Labs are graded on a ten-point scale
-
Lateness -- not an issue for lab 1, but lab 2 and on, 1/2 of a point per
day, up to 10 days. After 10 days, the lab will not be accepted
-
After lab 2, grading will not only be for correctness, but also style:
-
Function style -- not all out on one line, but several lines and nicely
indented.
-
Naming -- choosing good names for the functions and identifiers.
-
Comments -- having good comments describing your ML programs.
-
Up to 2.5 points can be deducted for style issues.
Newton to pounds conversion was backwards. 4.45 newtons = 1 pound.
-
On the moon, your weight in pounds will be about 1/6 your weight on earth
Difference between - and = prompts;
-
= means that ML is expecting more from the previous line. You haven't finished
the expression.
-
If you need to just cancel everything above, use ; repeatedly until you
get back to -
Use/Edit cycle for function files.
-
Need to save the file in the editor, then do the use "filename" command
again
-
Yes, this adds redefinitions, but it doesn't affect anything
Knowing what use has processed. Finding errors.
-
When you use a file, ML prints out its replies for each of the
definitions.
-
If there is an error, look for the last correct definition that ML responded
to.
-
Then try to understand the error. Usually just a spelling, or forgot a
;, ....
Math.sqrt and Modules, or Packages
-
Lots of different types and lots of desired built-in functions.
-
If every built-in function just went by a simple name, could easily have
clashes
-
So, built-ins are grouped into modules, and for many, the module
name is used as a prefix to the function name.
-
Square root is in the Math module, so you call it using Math.sqrt
-
We'll see more modules later
-
can use signature s=PACKAGE; to find all module functions.
Real number inequality
-
Expression list had you do 4.3<>5.4 -- why didn't this work?
-
Well, computers cannot store exact real numbers, only approximations.
-
So, why should a programming language let you test for exact equality?
-
ML does have Real.!= and Real.== if you really want to. But it's not good
in general
Parameter typing
-
The book only types one (or none) parameter, and lets ML figure out the
rest
-
You should always type all parameters. It's just easier to read later and
tell what the function is doing
Function definition structure and indentation
-
Use min/max as an example
Multiple expression list - not really useful yet
Testing your functions
-
How to pick values to test?
-
One test doesn't mean your function is correct
-
Use max3 as example, without the last test
Back to types
-
We have seen that all values in ML have types, like real, integer, string,
boolean, etc.
-
The real world also has types
-
pounds, ounces, kilograms, feet, meters, inches, gallons, liters, dollars,
pesos
-
name, address, ssn, phone, zip code, color
-
you can't simply add two values of two different types (5 pounds + 10 kilograms?)
-
some you can convert between, some you can't
-
1 pound = 16 ounces, but gallons to feet? zip code to ssn?
-
The real world is the problem domain -- the world in which the problem
is defined.
-
ML, or any other programming language, is the solution domain -- the virtual
computation environment where a computation is going to solve the real
world problem
-
The types of the problem must be mapped onto types in solution domain
-
pounds to a real number, name to a string, color to a string, feet to a
real number
-
Type consistency for the problem types must be maintained by you, the programmer
-
ML will make sure you don't add an integer and a real, but if you are using
a real to represent pounds and a real to represent kilograms, ML will not
stop you from adding pounds and kilograms!
-
This is just part of the programming endeavour.
Tuples
-
In Lab 2, had functions dealing with points, but simply used two identifiers
for the x and y coordinates.
-
ML has a data type that groups sets of values, called a tuple. A
tuple is an ordered set of data values; the values do not have to be the
same type. A tuple with three values is called a three-tuple. In general,
N values makes an N-tuple.
-
A tuple is strongly typed, just like simple values are. Each value is a
specific type, and the type of the tuple is the ordered types of its values,
with a * in between.
-
This is the same notation for function types
-
In ML, a tuple is surround by parentheses, and the values are separated
by commas
-
For Lab 2, a point could have been a two-tuple of two real numbers, like
(3.4,5.4), where the first number is the x coordinate and the second is
the y coordinate
-
Then, the distance function would take two tuples as arguments, rather
than four separate values
-
A function can return a tuple as well -- the body becomes a series of comma-separated
expressions, each computing the value for its place in the returned tuple.
-
Tuples can act as records of data - ("Cook,","Jonathan","123 Any St","Las
Cruces","NM",88005)
Program Design
-
Map the problem data types to ML data types
-
Break the problem computations into specific functions
Comments.
-
In ML, comments being with (* and end with *)
-
Comments help you remember what your program is supposed to do, and helps
others understand what it is doing.
-
Comments should not simply rephrase the ML computation, but should describe
what is being computed as it pertains to solving the real world problem.