B.12 Input and Output

I/O is among the impure features of ML since I/O in ML involves side effect.

B.12.1 Input

The option data type has two values: NONE and SOME. Use isSome (χ) to determine the value of a variable of type option. Use valOf (χ) to extract the value of a variable of type option. A string option list is not the same as a string list.

Standard Input

The standard input stream generally does not need to be opened and closed.

A set of three code lines in M L that is a standard input stream.
Description

File Input

The following example demonstrates file input in ML.

A set of 25 code lines in M L for demonstrating file input.
Description

B.12.2 Parsing an Input File

The following program reads a file and returns a list of strings, where each string is a line from the file:

A set of five code lines in M L for reading a file and returning a list of strings.
Description
Continuation of the code in M L for reading a file and returning a list of strings, consisting of 33 lines.
Description

B.12.3 Output

Standard Output

The print command prints strings to standard output:

A set of five code lines in M L with the print command.
Description

Use the functions Int.toString, Real.toString, etc. to convert values of other data types into strings.

File Output

The following transcript demonstrates file output in ML.

A set of four code lines in M L for demonstrating file output.
Description

Programming Exercises for Appendix B

Exercise B.1 Define a recursive ML function remove that accepts only a list and an integer i as arguments and returns another list that is the same as the input list, but with the ith element of the input list removed. If the length of the input list is less than i, return the same list. Assume that i = 1 refers to the first element of the list.

Examples:

A set of 12 code lines in M L with the function remove.
Description

Exercise B.2 Define a recursive ML function called makeset that accepts only a list of integers as input and returns the list with any repeating elements removed. The order in which the elements appear in the returned list does not matter, as long as there are no duplicate elements. Do not use any user-defined auxiliary functions, except member.

Examples:

A set of eight code lines in M L with the function make set.
Description

Exercise B.3 Define a recursive ML function cycle that accepts only a list and an integer i as arguments and cycles the list i times. Do not use any user-defined auxiliary functions.

Examples:

A set of 16 code lines in M L with the function cycle.
Description

Exercise B.4 Define an ML function transpose that accepts a list as its only argument and returns that list with adjacent elements transposed. Specifically, transpose accepts an input list of the form [e1, e2, e3, e4, e5, e6 … , en] and returns a list of the form [e2, e1, e4, e3, e6, e5, … , en, en–1] as output. If n is odd, en will continue to be the last element of the list. Do not use any user-defined auxiliary functions and do not use @ (i.e., append).

Examples:

A set of eight code lines in M L with the function transpose.
Description

Exercise B.5 Define a recursive ML function oddevensum that accepts only a list of integers as an argument and returns a pair consisting of the sum of the odd and even positions of the list. Do not use any user-defined auxiliary functions.

Examples:

A set of 13 code lines in M L with the function odd even sum.
Description
Continuation of the code in M L with the function odd even sum, consisting of three lines.
Description

Exercise B.6 Define a recursive ML function permutations that accepts only a list representing a set as an argument and returns a list of all permutations of that list as a list of lists. Try to define only one auxiliary function and pass a λ-function to map within the body of that function and within the body of the permutations function to simplify their definitions. Hint: Use the ML function List.concat.

Examples:

A set of 23 code lines in M L with the function permutations.
Description

Hint: This solution requires approximately 15 lines of code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.96.94