Specifying the Problem

Problem: write a function split which takes a list and divides it into two lists of equal length -`split' down the middle. For example split([1,2,3,4] should give ([1,2],[3,4]);

Now there is a problem with this `specification': What should our function do if the list has an odd length? The first thing we need to do when solving a problem is to make sure that the problem is well-defined. Clearly here it is not. So let's have another go:

Problem: write a function split which takes a list and divides it into two lists of equal length if the list's length is even. if the list's length is odd then the right hand list should have the extra element. For example split([1,2,3,4] should give ([1,2],[3,4]) and split([1,2,3,4,5] should give ([1,2],[3,4,5])
That's better! Now let's try and solve the problem using top-down design.



s.danicic@gold.ac.uk
Sebastian Danicic BSc MSc PhD (Reader in Computer Science)
Dept of Computing, Goldsmiths, University of London, London SE14 6NW
Last updated 2010-12-29