Exercise 1          Name _____________________  Pts ___/12

Write the functions that return the following results:

  1. g)inc 4 returns 5;

    fun inc x = x + 1;

    hM
  2. Modulus operation

    modulus(5,2)
    returns 1
    modulus(14,3) returns 2
    modulus(12,4) returns 0

    fun modulus(a,b) = a - (a div b) * b;

                    OR

    fun modulus(a,b) = a mod b;

     
  3. Euclid's Algorithm for greatest common denominator

    gcd(13, 5)
    the greatest common divisor, returns 1
    gcd(10,4) returns 2
    gcd(36,15) returns 3

    fun gcd(a,b) = if b = 0 then a else gcd(b, modulus(a,b));
  4.  
  5. h)second ["hello","world"]; returns "world“
    second [4, 5, 3];
    returns 5

    fun second L = hd (tl L);
  6. i)smallest [5, ~4, 3]; returns ~4

    fun smallest L =
        if null (tl L)
        then hd L
        else if hd L < smallest (tl L)
             then hd L
             else smallest (tl L);
       
  7. j)snoc 3 [4,5] returns [4, 5, 3];

    fun snoc a L = L@[a];

      OR recursively

    fun snoc a L =
        if null L
        then [a]
        else hd L::snoc a (tl L);