CS 171 Spring 2000 Laboratory Assignment # 2
Date Due:
-
No later than Monday 2/7/2000
Purpose: Introduction to the ML Environment with the following sub-topics:
-
evaluating on-line expressions in ML
-
writing functions in ML
Assignment
Complete the exercises below. Write your answers in the areas provided.
Attach a copy of your ML program to this lab handout and give it to your
lab instructor.
Getting Started in ML
-
Log on to a Linux host.
-
Start ML by entering the following at the command prompt:
sml
-
From lecture material and the textbook, answer the following questions:
-
How can an expression be evaluated on-line in ML?
-
What purpose does the semicolon serve in ML?
-
How do you quit the ML system?
-
Evaluate the following expressions in ML and write the result next to the
expression (remember to put a semicolon after each expression):
-
2 + 4 * 5 < (2 + 4) * 5
-
"read" ^ "y"
-
"b" = "B"
-
225.0 / 0.5
-
240 div 2 div 3
-
120 / 4.0
-
trunc(2.5793) * 2.5
-
chr(68) ^ chr(72)
-
9 + 4 * (-6)
-
floor(9.345) * 3
-
if 2.4 <> 3.5 then 0 else 1
-
if "grape" < "gripe" then "yes" else "no"
Fixing an existing function definition
Copy a program from the instructor's home directory by typing the following
command at the linux prompt:
cp ~gupta/public_html/Classes/CS171/lab2.sml lab2.sml
The file lab2.sml contains a program for finding the maximum of 2 numbers.
Invoke the ML interpreter by typing:
sml
(if you get the message: "command not found" on typing sml, then first
type the command "source /local/config/cshrc.ml")
To load the program from the file lab2.sml type the following command
at the - prompt of the sml interpreter.
use "lab2.sml";
Now type the following expressions at the - prompt of the sml interpreter
max(2,4);
max(10,4);
Observe the answer reported in both cases. Are the answers in the two
cases correct?
If not, then examine the file lab2.sml, and fix the function max. Use
xemacs to edit the file lab2.sml. You can
start the emacs editor in order to modify the file lab2.sml by
typing the following at the linux prompt:
xemacs lab2.sml &
Writing functions in ML:
Create definitions for the following functions described below. Add
the definitions to the file lab2.sml (To add these new function,
just edit the file lab2.sml using xemacs). Be sure to test all of
your functions thoroughly.
-
Create a function for determining the square of a real number.
-
Create a function for finding the distance between two points (x1, y1)
and (x2,y2) given the formula for distance: d = sqrt( (x1 - x2) 2 + (y1
- y2)2 ). All values are real numbers.
-
Create a function to find the area inside a circle given the coordinates
of the center and a point on the circle. Call the functions you created
in # 1 and # 2 from within this function.
-
Create a function to determine your weight (in pounds) on the moon. Include
a call to the function you wrote for # 1. You will need the following formula:
Weight on the Moon in newtons = Gravitational constant * mass of moon
in kilograms * your mass in kilograms / (Radius of the moon in kilometers)^2
You may also need some of these constants:
-
1 kilogram = 0.4536 earth-pounds
-
1 earth-pound = 4.45 newtons
-
Mass of the moon in kilograms = 7.352E22
-
Radius of the moon in kilometers = 1738
-
Gravitational constant = 6.672E~17
Note: the built-in function Math.sqrt
should be used in the function for problem 3