Elements of ML Programming, 2nd Edition (ML97)

Solutions for Chapter 2

Solutions for Section 2.1

Solutions for Section 2.2

Solutions for Section 2.3

Solutions for Section 2.4

Solutions for Section 2.1

Exercise 2.1.1(a)

val it = 7 ; int

Exercise 2.1.1(c)

val it = 2 : int

We group operators from the left, so we first evaluate 11 div 2, or 5, and then evaluate 5 mod 3, which is 2.

Exercise 2.1.1(e)

val it = false : bool

The andalso groups its operands before the orelse. Thus, the whole expression is grouped

     3>4 orelse (5<6 andalso (not 7<>8))

Exercise 2.1.1(g)

val it = 294 : int

Note that AB in hexadecimal is 171 in decimal.

Exercise 2.1.2(a)

The / operator applies only to reals, not integers. Use div instead.

Exercise 2.1.2(c)

The and operator has a meaning not discussed in Section 2.1; it cannot be applied to boolean values. Use andalso instead.

Exercise 2.1.2(e)

A real number needs to have digits both before and after the decimal point. Instead of 4., use 4.0.

Exercise 2.1.2(g)

The concatenation operator ^ applies to strings, not characters. "a"^"b" would be correct.

Exercise 2.1.2(i)

Real numbers may not be compared using = or <>. A legal equivalent would be
     1.0 <= 2.0 andalso 2.0 <= 1.0

Exercise 2.1.3

There are many ways to break the string. Here is one:
"\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.

Exercise 2.1.4(a)

     if E then true else F
<\PRE>

Return to Top

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.

Return to Top

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).

Return to Top

Solutions for Section 2.4

Exercise 2.4.1(a)

4

Exercise 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) list

Exercise 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")

Return to Top