- 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 exampledouble 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. - 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); - Evaluate the expression "one"^"one" . Define the function duplicate such
that
duplicate "go" evaluates to "gogo" Also define quadricate, octicate and hexadecicate. Hints:#### Reflection

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. Specifically we let*x*be 5 here.*x*`=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*10`Use the equation for double again. `=20` - 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 usersThe 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 heremiddle "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