Elements of ML Programming, 2nd Edition (ML97) |
We group operators from the left, so we first evaluate 11 div 2, or 5, and then evaluate 5 mod 3, which is 2.
The andalso groups its operands before the orelse. Thus, the whole expression is grouped
3>4 orelse (5<6 andalso (not 7<>8))
Note that AB in hexadecimal is 171 in decimal.
1.0 <= 2.0 andalso 2.0 <= 1.0
"\t\"\\\\\\\" stands for the double-quote \ \character, \\\n\t\\which otherwise \ \would be interpreted \\\n\t\\as the \ \string ender.\""
If you didn't ``get this,'' now that you see the tricks, try creating a string that will print the four displayed lines above.
if E then true else F <\PRE>Solutions for Section 2.2
Exercise 2.2.1(a)
floor(123.45) works. We could also use trunc or round as the operator in this example.Exercise 2.2.1(d)
ceil(~123.45) works. Operators trunc and round would also work in this case.Exercise 2.2.1(e)
ord(#"Y")Exercise 2.2.1(g)
real(ord(#"N"))Exercise 2.2.2(a)
The function ceil requires a real argument, e.g., ceil(4.0)Exercise 2.2.2(c)
The argument of chr must be in the range 0 to 255. Thus, 256 is not an acceptable argument. It is not clear how to fix this expresion, since there is no obvious intent.Exercise 2.2.2(e)
Function ord must take a character as an argument. Perhaps chr(#"3") was meant. The result would be 51, the ASCII code for character #"3".Exercise 2.2.2(h)
Again, ord requires a character as argument. Now, probably ord(#"a") was meant.Solutions for Section 2.3
Exercise 2.3.1(a)
An alphanumeric identifier suitable for ordinary values.Exercise 2.3.1(c)
Not an identifier. The comma is not permitted in identifiers of any kind.Exercise 2.3.1(e)
Not an identifier. The characters of symbolic and alphanumeric identifiers may not be combined in a single identifier. a<=b is interpreted as a sequence of three identifiers: a, <=, and b.Exercise 2.3.1(g)
Not an identifier for the same reason as Exercise 2.3.1(e).Solutions for Section 2.4
Exercise 2.4.1(a)
4Exercise 2.4.1(c)
[4,5]Exercise 2.4.1(e)
"foo"Exercise 2.4.1(g)
["c", "o", "b", "o", "l"]Exercise 2.4.2(a)
There is no fourth component. The number following # must be 1, 2, or 3 for this tuple.Exercise 2.4.2(c)
(1) is not a tuple; it is a parenthesized integer. Thus, the # operator may not be applied to (1).Exercise 2.4.2(e)
Function implode applies only to lists of characters. Here, the argument is a tuple of characters. Perhaps implode([#"a", #"b"]) was meant.Exercise 2.4.2(g)
Function tl cannot be applied to the empty list, whose tail is not defined.Exercise 2.4.2(i)
Function concat applies to lists of strings, not lists of characters. Perhaps implode instead of concat was intended, or the characters were intended to be strings, as concat(["a","b"]).Exercise 2.4.3(a)
real * (string * (int list))Exercise 2.4.3(c)
(int * real) listExercise 2.4.4
(1,2) and (1,2,3) are not of the same type. (1,2) is of the type int * int, and (1,2,3) is of type int * int * int. However, [1,2] and [1,2,3] are of the same type: int list.Exercise 2.4.5(a)
[[[1,2],[3,4]], [[5,6],[7,8]]]Exercise 2.4.5(c)
(["a","b"], (1,(2.5,"c")), 3)Exercise 2.4.5(e)
((true,1), #"a")