Grzegorz Bancerek

Paradigms of Programming in O'Caml

Solutions of Exercises 2: Arrays

Manual: Module: Array
  1. Define the Sieve of Eratosthenes
      let eratosthenes n = let a = Array.make (n+1) true in
        begin
          for i = 2 to n/2 do
            let m = ref (i+i) in
            while (!m) <= n do
              a.(!m) <- false;
              m := (!m)+i
            done;    
          done;
          for i = 2 to n do
            if a.(i) then (print_int i; print_string ", ")
          done
        end;;
    
  2. Define the sum (sigma) of all elements from an array.
  3. Define the collective operation on f-images of all elements from an array.
  4. Define the value of a polynomial at an argument.
  5. Operations on polynomials: addition, multiplication, division.
  6. Multiplication of matrices.
      C.(i).(j) = sum 0 n (fun k -> A.(i).(k)*B.(k).(j))
    
  7. Determinant.
      let det2 A = ...
    
      let rec det (* n = Array.length A *) A = ... uses minors
    
  8. Insertion sort.
  9. Bubble sort.
  10. Selection sort.
  11. Merge sort.
  12. Quick sort.
  13. Heap sort.

bancerek@wi.pb.edu.pl