CS 171 Spring 2000 Laboratory Assignment # 2

Date Due:
  1. No later than Monday 2/7/2000
Purpose: Introduction to the ML Environment with the following sub-topics:
  1. evaluating on-line expressions in ML
  2. 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

  1. Log on to a Linux host.
  2. Start ML by entering the following at the command prompt:

  3. sml
     
  4. From lecture material and the textbook, answer the following questions:
    1. How can an expression be evaluated on-line in ML?

    2.  

       

    3. What purpose does the semicolon serve in ML?

    4.  

       

    5. How do you quit the ML system?

    6.  

       

    7. Evaluate the following expressions in ML and write the result next to the expression (remember to put a semicolon after each expression):

    8.  

       

      1. 2 + 4 * 5 < (2 + 4) * 5

      2.  

         

      3. "read" ^ "y"

      4.  

         

      5. "b" = "B"

      6.  

         

      7. 225.0 / 0.5

      8.  

         

      9. 240 div 2 div 3

      10.  

         

      11. 120 / 4.0

      12.  

         

      13. trunc(2.5793) * 2.5

      14.  

         

      15. chr(68) ^ chr(72)

      16.  

         

      17. 9 + 4 * (-6)

      18.  

         

      19. floor(9.345) * 3

      20.  

         

      21. if 2.4 <> 3.5 then 0 else 1

      22.  

         

      23. 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:

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:


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.

  1. Create a function for determining the square of a real number.
  2. 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.
  3. 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.
  4. 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:
  5. 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. 1 kilogram = 0.4536 earth-pounds
    2. 1 earth-pound = 4.45 newtons
    3. Mass of the moon in kilograms = 7.352E22
    4. Radius of the moon in kilometers = 1738
    5. Gravitational constant = 6.672E~17
    Note: the built-in function Math.sqrt should be used in the function for problem 3