Homework # 3
due Tuesday, February 17, 4:00 PM

Simple ML Programs

  1. Write a function scale that takes a real and a list of reals and returns a list which multiples the number to every element in the list. For example:
    
    - scale (3.0,[1.0,2.0,3.0]) ;
    val it = [3.0,6.0,9.0] : real list
    
  2. Write a function dot that takes two lists of real numbers of the same length and returns the ``dot product'' (the sum of the element-wise multiplication). For example:
    
    - dot ([1.0, 2.0],[~3.0, 4.0]) ;
    val it = 5.0 : real
    
    Your program need not be well-behaved when the lists are not the same length.
  3. Write a function magn that takes a list of real numbers and returns the square root of the sum of the squares of the elements. Hint, you can use dot to do most of the work.
    
    - magn [1.0,1.0,1.0,2.0,3.0] ;
    val it = 4.0 : real
    
    (You will need to ``open Math'' to make the sqrt function available.)
  4. Use all these functions to define a function project that ``dots'' the two list arguments together and divides the result by the square of the magnitude of the second, and then uses the result to scale the second. This operation projects the first vector onto the second.
    
    - project ([1.0,1.0],[2.0,0.0]);
    val it = [1.0,0.0] : real list
    
Put all your functions in a single file in homework3 named vector.sml.

Types

Do the following problems about types:

  1. Exercise 1 of Chapter 6 (page 100-101).
  2. Investigate and report on these varieties of data types. Describe your findings fully and don't forget to discuss representation issues and supported operations:
Turn your answers in on paper at the beginning of lecture.

Submitting Your Work

You submit your program work by putting it in the homework3 directory in your AFS class volume. You may do all your work in this directory, or you may wish to do your work in a different directory and copy things when correct into this directory. In any case, you should not change anything in this directory after the deadline, which is 4:00pm on Tuesday, February 17th. In other words, you must be done before lecture starts.

The homework3 directory should include the following:


About this document



John Tang Boyland
2004-02-10