GIML: Tutorial Six: Answers

  1. Output for each of the maps
    ["patio","studio","ratio"] : string list
    [[4],[2],[1]] : int list list
    [2,7,8] : int list
    ["l","a","s","t"] : string list
    
  2. Defining functions using map
    val ftrl = map (fn x=>3*x)
    val fhel = map (hd o explode)
    val fttl = map (implode o tl o tl o explode)
    val fsml = map (fn x=> "s"^x^"m")
    
  3. Determining the effect of various functions:
    r
    This is the the function rev which reverses a list
    r [1,2,3] = [3,2,1]
    p
    This function is simply append with the arguments reversed.
    p [1,2,3] [4,5,6] = [4,5,6,1,2,3]
    dr
    This may be used to reduce a string of decimal digits to a single number. If the elements of the input list are not between 0 and 9 the output is less obvious.
    dr [3, 6, 2] = 263
    m
    This is the member function, m x l returns true if x is in the list l, otherwise it returns false.
    m 3 [2,6,7]   = false
    m 3 [2,3,6,7] = true
    m 3 [3,3,3,3] = true
    
    n
    n x l returns true if all of the elements of l are equal to x.
    n 3 [2,6,7]   = false
    n 3 [2,3,6,7] = false
    n 3 [3,3,3,3] = true
    
    im
    This is implode
    im ["a", "b", "c"] = "abc"
    ts
    This is tospace it returns the elements of l upto the first (single) space.
     ts(explode "One fine day") = ["O","n","e"] : string list
    
  4. val prodlist = reduce (op * ) 1
    val flatten = reduce (op @) nil
    val count = reduce (fn(a,b)=>1+b) 0
    val duplist = reduce (fn(a,b)=>a::a::b) nil
    
  5. rm
    rm x l returns the list l with all occurances of x removed
    mx
    returns the maximum element of l
    returns the list with each element squared
    sieve
    sieve(upto 2 500) returns all prime numbers from 2 to 500.