GIML: Tutorial Four: Answers

  1. Simply evaluate sum and doublist over the input [5, 3, 1]
    sum [5, 3, 1] = 9 : int
    doublist [5, 3, 1] = [10, 6, 2] : int list
    

  2. fun len nil   = 0
    |   len(h::t) = 1 + len t;
    fun triplist nil   = nil
    |   triplist(h::t) = 3*h :: triplist t;
    fun duplist nil   = nil
    |   duplist(h::t) = h::h::duplist t;
    fun product nil   = 1
    |   product(h::t) = h * product t;
    

  3. fun vallist nil = nil
    |   vallist(h::t) = (ord h - ord "0") :: vallist t;
    

  4. fun rev nil  = nil
    |   rev(h::t)= (rev t) @ [h];
    

  5. fun space nil   = nil
    |   space(h::t) = h::" "::space t;
    
    fun flatten nil  = nil
    |   flatten(h::t) = h @ flatten t;
    
    fun count_1's nil    = 0
    |   count_1's (1::t) = 1 + count_1's t
    |   count_1's (h::t) = count_1's t;
    
    fun timeslist x nil   = nil
    |   timeslist x(h::t) = (x*h : int)::timeslist x t;
    
    fun last(h::nil)= h
    |   last(h::t)  = last t;
    
    fun member x  nil  = false
    |   member x(h::t) = (x=h) orelse member x t;