GIML: Tutorial Five: Answers

  1. Each of these functions refer to items in lists by the position within the list. In each case the first item in the list (i.e. the head of the list) is item zero.
    index(n, l) This returns the item at position n. There is a similar "built-in" function called nth.
    takeN(n, l) This returns the first n items from the list l.
    dropN(n, l) This returns the list l with the first n items missing.
    Examples
    index(2, [100,200,300,400]) = 300
    takeN(3, ["one","two","three","four","five"])= ["one","two","three"]
    dropN(3, ["one","two","three","four","five"])= ["four","five"]
    
  2. Sorting fun insert (n:int) nil = [n] | insert n (h::t) = if (n<h) then n::h::t else h::insert n t; fun sort nil = nil | sort(h::t) = insert h (sort t);
  3. upto fun upto m n = if n<m then nil else m::upto (m+1) n;
  4. See Language processing