The beauty of treemap

One of my main takeaway of the book study group I organised within Coders Only was the simplicity of the treemap function, which takes a tree and a function as an input, and returns a new tree, where all the nodes are the result of inputting the original node through the function.

Note to myself: use recursion when parsing through a tree.

  • Trees are beautiful and the way to deal with two-dimensional data
(define (treemap fn tree)
  (cond ((null? tree) '())
    ((leaf? tree) (fn tree))
    (else (map (lambda (t) (treemap fn t)) tree)) ))

Note: the map function takes a function and a list, applies the function to all the elements, and returns a new modified list.