GIML: Tutorial Two: answers
- In each case ML assumes the most general type possible. Note the
difference between a tuple and a list.
("two",true,2) : string * bool * int
["two", true, 2] this is an illegal list (it is not well formed)
((1,2),(3,4,5)) : (int * int) * (int * int * int)
[[1,2],[3,4,5]] : int list list
[[],[],[]] : 'a list list
("andrew", size "andrew") : string * int
(["andrew"="andrew","andrew"="ben"],size "andrew", size)
: bool list * int * (string -> int)
In the last example note that the two equations are evaluated to true
and false respectively, together they make a bool list.
Note also that the unevaluated function size on its own is a
propery object which may be part of a tuple.
- You can simply enter these into ML.
Note that the binding
val h::t = [1,2,3];
Leads to a warning on some systems, whereas the binding
val (l,m,n) = ("xx",(1,2));
results in an error, the types on the left and the right of
the equals sign are incompatible.
-
fone : int -> int list
ftwo : 'a -> 'a * 'a * 'a
fthree : (string*string) -> string list
ffour : int * string * 'a -> int * 'a
-
"s" "o" "t" "s"
-
val third = hd o tl o tl o explode;
val fourth = hd o tl o tl o tl o explode;
val last = hd o rev o explode;
- It is not always easy to work out the chain of functions for these
permutations.
val fb = roll o roll o roll;
val fc = roll o exch o fb;
val fd = exch o fc o exch;