Homework # 3
due February 15, 3:30 PM

Vectors

Mathematical vectors in $n$-dimensional Euclidean space can be represented by lists of real numbers. For this homework, you will define several operations on vectors:

  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, also known as the ``inner product''). For example:
    - dot ([1.0, 2.0],[~3.0, 4.0, 6.0]) ;
    val it = 5.0 : real
    
    As seen here, your program should assume a shorter vector is extended with zeros as necessary.
  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. Define an addition function for vectors:
    - add ([1.0, 2.0], [~5.0, 0.0, 1.5]) ;
    val it = [~4.0, 2.0, 1.5] : real list
    
Put all your functions in a single file named vector.sml in your AFS volume under directory homework3.

Types

Do Exercises 1 and 2 of Chapter 6 (pages 101-102). Write your answers on paper.

Submitting Your Work

Turn in the first part in AFS, and the second part on paper at the beginning of lecture.


About this document



John Tang Boyland 2011-02-07