GIML: Tutorial One

Tutorial One: Expressions & simple functions

ML has a fairly standard set of mathematical and string functions which we will be using initially. Here are a few of them + integer or real addition - integer or real subtraction * integer or real multiplication / real division div integer division e.g. 27 div 10 is 2 mod remainder e.g. 27 mod 10 is 7 ^ string concatenation e.g. "cub"^"a" All of the above are infix. That is the operator appears between the two arguments.
  1. Define and test the functions double and triple. double is given below: fun double x = 2 * x; This function may be exercised by entering an expression to be evaluated. For example double 3; The function times4 may be defined by applying double twice. This is function composition. fun times4 x = double(double x); Use double and triple to define times9 and times6 in a similar way.
  2. Functions with more than one input may be defined using "tuples". Define and test aveI and aveR given: fun aveI(x,y) = (x+y) div 2; fun aveR(x,y) = (x+y)/2.0; Notice how ML works out the type of each function for itself. Try... aveR(3.1 , 3.5); aveI(31, 35);
  3. Evaluate the expression "one"^"one" . Define the function duplicate such that duplicate "go" evaluates to "gogo" Also define quadricate, octicate and hexadecicate. Hints:


    The ML interpreter has a very clear, simple operation. The process of interpretation is just that of reduction. An expression is entered at the prompt and is reduced according to a simple set of rules.
    Example: Evaluate times4(5)
    times4(5) =double(double 5) because
    times4 x=double(double x)
    for any value of x. Specifically we let x be 5 here.
    =double(2*5) we replace the sub expression double 5 with 2*5 as the equation for double permits.
    =double(10) We simply replace 2*5 with 10.
    =2*10Use the equation for double again.
  4. Some pre-defined functions are ord, chr, size, substring. The type or signature of each may be discovered by entering the name of the function alone: ord : string -> int chr : int -> string size : string -> int substring : string * int * int -> string Note for Moscow ML users

    The functions ord and chr convert characters to ASCII values and vice versa. size returns the number of characters in the string and substring accepts a string, the start position and length of the substring, note that the first character of the string is number zero. Suppose we wish to create the function clip which removes the last character from its input.

    clip "been" = "bee" clip "raven" = "rave" If s is the input string we need to return the substring starting at 0 of length "one less than the size of the input string" fun clip s = substring(s,0,size s - 1); Define the following functions given by example here middle "badge" = "d" middle "eye" = "y" dtrunc "trouser" = "rouse" dtrunc "plucky" = "luck" incFirst "bad" = "cad" incFirst "shin" = "thin" switch "overhang" = "hangover" switch "selves" = "vessel" dubmid "below" = "bellow" dubmid "son" = "soon"

Answers to tutorial 1
You may wish to try the self assessment exercise now.
Now would be a good time to try Diversion: The Reconciliation Ball