Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP712 Programming Languages

Programming Portfolio Part 2 (25%)

1. Streams in Racket (16%)

Task: Write the following Racket procedures. Note that the procedures defined later can make use of the ones defined earlier.

(a) Write a procedure named sequence that takes 3 arguments – low, high and stride. All three arguments are numbers with stride assumed to be positive. This procedure should produce a list of numbers from low to high (inclusive) separated by stride and in sorted order. Usage Examples:

(sequence 3 11 2) Þ ’(3 5 7 9 11)

(sequence 3 8 3) Þ ’(3 6)

(sequence 3 2 1) Þ ’()

(b) Write a procedure named string-append-map that takes a list of strings xs and a string suffix as arguments. It should return a list of strings with each element corresponding to elements of xs appended with suffix with no extra space between them. You must use the Racket library functions map and string-append.

(c) Write a procedure list-nth-mod that takes a list xs and a number n as arguments. It should return the ith element of the list (counting from zero), and i is the remainder when n is divided by the length of the list.

If n is negative, terminate the procedure with:

(error “list-nth-mod: negative number”)

If xs is empty, terminate the procedure with:

(error “list-nth-mod: empty list”)

You may use the Racket library functions length, remainder and list-tail.

(d) Write a procedure stream-for-n-steps that takes a stream s and a non-negative number n as arguments. It should return a list with the first n values produced by s in order. You may use this procedure to test the streams you write for later parts.

(e) Write a stream funny-number-stream that is a stream of natural numbers except those numbers divisible by 5 are negated, i.e. 1, 2, 3, 4, -5, 6, 7, 8, 9, -10, 11, …

Note that a stream is a thunk that when called produces a pair. Here, the car of the pair will be a number and the cdr is another stream.

(f) Write a stream dan-then-dog where the elements of the stream alternate between the strings “dan.jpg” and “dog.jpg”. It should be a thunk that when called, produces pair of “dan.jpg” and a thunk which when called, produces “dog.jpg” and a thunk, etc.

(g) Write a procedure named stream-add-zero that takes a stream s as argument and returns another stream. If s would produce v for its ith element, then (stream-add-zero

s) should produce the pair (0. v) as its ith element.

(h) Write a procedure named cycle-lists that take two lists xs and ys as arguments and returns a stream. The lists may or may not be of the same length, but you may assume that both of them are not empty. The elements produced by the stream are pairs where the first part is from xs and the second part is from ys. The stream cycles forever through the lists. For example, if xs is ’(1 2 3) and ys is ’(“a” “b”), then the stream should produce (1 . “a”), (2 . “b”), (3 . “a”), (1 . “b”), (2 . “a”), (3 . “b”), etc.

2. Logic Programming (9%)

Based on the parent relationship defined in the lecture, do the following:

(a) Expand the database to include facts that a name is a male or a female.

(b) Define a predicate %sister that determines if two persons are sisters.

(c) Define a predicate %predecessor that determines if a person is the predecessor (parent or grandparent) of another person.

Submission: Your submission should consist of all the Racket source files. The files for each of the two questions must be put into two directories. Archive (i.e. zipped) your programs into a single file with your surname as the filename.