1) Lazy Sequencing problem:

Write a function in Clojure that outputs a list of numbers 1 through 10. Hint: there is an iterate function, and a take function in Clojure. This demonstrates lazy sequencing in Clojure as the iterate function is an infinite sequence but it uses a small, finite amount of memory.

Example output:

(1 2 3 4 5 6 7 8 9 10)

The `iterate’ function: (iterate f x) – generates an infinite sequence x, f(x), f(f(x)), f(f(f(x))), and so on.

Actually, there is nothing infinite about this sequence. We have a simple two element sequence in memory. The first element is the ‘x’ and the next element is a function that will compute the succeeding elements of the sequence, when required. When we request the second element of the sequence, this function computes the second element plus yet another function which will yield the remaining elements of the sequence and so on.

2) Write a function in Clojure that calculates the cube of a parameter

3) Define a function less so that (less [e,L]) is a list if all the integers L that are less than e.

4) Write a Clojure function that computes the nth permutation of a list parameter. The function header should read (defn permute [n mylist] (;code)) where n is the desired nth permutation, and mylist is the list to be permuted. By nth, we mean to say in lexicographical order, the nth item.

General algorithm:

• Next character is at mylist[(n/(size(mylist) – 1)!]

• Remove the character at this index and recur with [(n-1)!/(mod n), updated mylist].

5) Using the permutation function from before, write a set of functions to solve the Traveling Salesman problem in (O(n!)) time.

Use the Permute function to generate a list of all possible routes to follow between the 5 points.

Here is a listing of the distances between 5 points:

[[0,21,41,30,43],[21,0,21,19,24],[41,21,0,20,20],[30,19,20,0,3.6],[43,24,20,36,0]]

You should also define the following helper functions

(distance [route]) to compute the distance along a route. Route is a list of locations between the 5 points.

# Sample Problems

Posted by Patrick Connelly
March 31, 2011

## Add a comment