recursionfunctional-programmingschemelispseasoned-schemer

How does scramble function works? (Chapter 1 of The Seasoned Schemer)


According to the book, this is what the function definition is,

The function scramble takes a non-empty tuple in which no argument is greater than its own index and returns a tuple of same length. Each number in the argument is treated as a backward index from its own position to a point earlier in tuple. The result at each position is obtained by counting backward from the current position according to this index.

And these are some examples,

; Examples of scramble
(scramble '(1 1 1 3 4 2 1 1 9 2))       ; '(1 1 1 1 1 4 1 1 1 9)
(scramble '(1 2 3 4 5 6 7 8 9))         ; '(1 1 1 1 1 1 1 1 1)
(scramble '(1 2 3 1 2 3 4 1 8 2 10))    ; '(1 1 1 1 1 1 1 1 2 8 2)

Here is the implementation,

(define pick
  (λ (i lat)
    (cond
      ((eq? i 1) (car lat))
      (else (pick (sub1 i)
                  (cdr lat))))))

(define scramble-b
  (lambda (tup rev-pre)
    (cond
      ((null? tup) '())
      (else
       (cons (pick (car tup) (cons (car tup) rev-pre))
             (scramble-b (cdr tup)
                         (cons (car tup) rev-pre)))))))

(define scramble
  (lambda (tup)
    (scramble-b tup '())))

Solution

  • This is a case where using a very minimal version of the language means that the code is verbose enough that understanding the algorithm is not perhaps easy.

    One way of dealing with this problem is to write the program in a much richer language, and then work out how the algorithm, which is now obvious, is implemented in the minimal version. Let's pick Racket as the rich language.

    Racket has a function (as does Scheme) called list-ref: (list-ref l i) returns the ith element of l, zero-based.

    It also has a nice notion of 'sequences' which are pretty much 'things you can iterate over' and a bunch of constructs whose names begin with for for iterating over sequences. There are two functions which make sequences we care about:

    And the iteration construct we care about is for/list which iterates over some sequences and collects the result from its body into a list.

    Given these, then the algorithm is almost trivial: we want to iterate along the list, keeping track of the current index and then do the appropriate subtraction to pick a value further back along the list. The only non-trivial bit is dealing with zero- vs one-based indexing.

    (define (scramble l)
      (for/list ([index (in-naturals)]
                 [element (in-list l)])
        (list-ref l (+ (- index element) 1))))
    

    And in fact if we cause in-naturals to count from 1 we can avoid the awkward adding-1:

    (define (scramble l)
      (for/list ([index (in-naturals 1)]
                 (element (in-list l)))
        (list-ref l (- index element))))
    

    Now looking at this code, even if you don't know Racket, the algorithm is very clear, and you can check it gives the answers in the book:

    > (scramble '(1 1 1 3 4 2 1 1 9 2))
    '(1 1 1 1 1 4 1 1 1 9)
    

    Now it remains to work out how the code in the book implements the same algorithm. That's fiddly, but once you know what the algorithm is it should be straightforward.