If you want to work on one of these, put your name in the block so we know someone's working on it. Then, change n in your block to the appropriate problem number, and fill in the <Problem description>,<example in lisp>,<example in Haskell>,<solution in haskell> and <description of implementation> fields.

Since there's already syntactic sugar for ranges, there's usually no reason to define a function like 'range' in Haskell. In fact, the syntactic sugar is implemented using the enumFromTo function, which is exactly what 'range' should be.

6 Problem 26

(**) Generate the combinations of K distinct objects chosen from the N elements of a list
In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the
well-known binomial coefficients). For pure mathematicians, this result may be great. But we want to really generate all the possibilities in a list.

Note that we do not want permutations of the group members; i.e. ((ALDO BEAT) ...) is the same solution as ((BEAT ALDO) ...). However, we make a difference between ((ALDO BEAT) (CARLA DAVID) ...) and ((CARLA DAVID) (ALDO BEAT) ...).

You may find more about this combinatorial problem in a good book on discrete mathematics under the term "multinomial coefficients".

from the above problem. Actually we need more than the elements we selected, we also need the elements we did not select. Therefore we cannot use the

tails

function because it throws too much information away. But in general this function works like the one above. In each step of the recursion we have to decide whether we want to take the first element of the list

(x:xs)

in the combination (we collect the possibilities for this choice in

ts

) or if we don't want it in the combination (

ds

collects the possibilities for this case).
Now we need a function

group

that does the needed work. First we denote that if we don't want any group there is only one solution: a list of no groups. But if we want at least one group with n members we have to select n elements of

xs

into a group

g

and the remaining elements into

rs

. Afterwards we group those remaining elements, get a list of groups

gs

and prepend

g

as the first group.

And a way for those who like it shorter (but less comprehensive):

group ::[Int]->[a]->[[[a]]]
group []=const[[]]
group (n:ns)=concatMap(uncurry$(. group ns).map.(:)). combination n

8 Problem 28

Sorting a list of lists according to length of sublists

a) We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of this list according to their length. E.g. short lists first, longer lists later, or vice versa.

b) Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements of this list according to their length frequency; i.e., in the default, where sorting is done ascendingly, lists with rare lengths are placed first, others with a more frequent length come later.

What we need is a function that takes a sublist and counts the number of other sublists with the same length. To do this, we first construct a list containing the lengths of all the sublists (called lengths above). Then the function frequency can just count the number of times that the current sublist's length occurs in lengths.