Page 2 of 2

Re: PROBLEM SCHEME

Posted: Tue Oct 13, 2015 3:30 am
by Goheeca
I would decompose it to two problems.
  1. filling in to the rectangle
  2. transposing
Rectangularization:

Code: Select all

(define (repeat n elem)
    (if (= n 0)
        '()
        (cons elem (repeat (- n 1) elem))))

(define (fill-list lst len elem)
    (if (>= (length lst) len)
        lst
        (append lst (repeat (- len (length lst)) elem))))

(define (make-rectangle data with)
    (map (lambda (row)
            (fill-list row (apply max (map length data)) with))
        data))
Transposing:

Code: Select all

(define (transpose data)
    (apply map list data))
Solution:

Code: Select all

(define (solution data)
    (transpose (make-rectangle data '())))
Here it is on repl.it.

What concerns recursive thinking, there are three main parts in a simple recursion:
  1. condition of termination -- e.g. null?
  2. tail value/elementar operation -- e.g. '()
  3. recursive computation itself (processing of an element + compounding of the results)
  4. -- e.g. [i](cons (process ...) ...)[/i]
During the construction of an recursive function try to ask how these parts look like and then it should be easy.