How to Annotate an S-List

Putting Several Patterns Together

An annotate Function for s-lists

Recall the annotate function from Session 10. It takes a flat list of symbols, such as (ball-state michigan-state northern-iowa), and returns a list of symbol/position lists, ((ball-state 1) (michigan-state 2) (northern-iowa 2)). Solving this problem required a new idea, the interface procedure.

Then we learned about a new data type, the s-list, which allows symbols in the list to be nested inside of other lists. S-lists are useful to us because they resemble Racket programs, which means that learning how to process s-lists helps us learn how to process programs of this form.

Can we annotate an s-list? Sure. Instead of a symbol's position in any particular list, we would probably care more about its depth in the tree. For example:

> (annotate-slist '(ball-state michigan-state northern-iowa))
'((ball-state 0) (michigan-state 0) (northern-iowa 0))

> (annotate-slist '((a b) (((b g r) (f r)) c (d e)) b))
'(((a 1) (b 1))
  ((((b 3) (g 3) (r 3)) ((f 3) (r 3))) (c 1) ((d 2) (e 2)))
  (b 0))

Writing annotate-slist

Try to write annotate-slist now. This problem is an application of three techniques we have studied: structural recursion, mutual recursion, and interface procedures.

When you are done, press the button to see a solution...