Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 2.3
Restructuring of lists ***


Take a look at a list lst, such as

  (a b c d e f g h i)

Write a function (sublist-by-rows n lst) that restructures this list to a list of lists, with the same elements as in lst. n is the number of elements in each sublist. As an example (sublist-by-rows 3 lst) should be

  ((a b c) (d e f) (g h i))

If we consider the sublists as rows in a table on a webpage, the following expression

  (render (simple-html-table 40 (sublist-by-rows 3 (map as-string lst))))

renders this structure as

a b c
d e f
g h i

The function simple-html-table is a convenient function which applies the necessary table, tr, and td elements on the elements in the rows. The first parameter of the function controls the column width. We will develop this function in a later part of this material .

In many web contexts, it would be useful to have a function similar to sublist-by-rows which produces columns instead of rows. Let us call this new function for sublist-by-columns. Thus (sublist-by-columns 3 lst) should be

  ((a d g) (b e h) (c f i))
Now program sublist-by-column. Notice that
  (render (simple-html-table 40 (sublist-by-columns 3 (map as-string lst)  )))

will be rendered as the table

a d g
b e h
c f i

in HTML.

Demonstrate some practical uses of your functions in the web domain. I.e., restructure some lists of information to table rows, and write a simple web page in LAML which shows the tables.


There is no solution to this exercise