## Tuesday, April 30, 2013

### SICP 2.66: Sets and information retrieval

From SICP section 2.3.3 Sets and information retrieval

The final part of section 2.3.3 asks us to consider a database that contains records, each of which has a key and some data. If the database is represented as an unordered list, a record can be looked up by its key using the following procedure.

(define (lookup given-key set-of-records)
(cond ((null? set-of-records) false)
((equal? given-key (key (car set-of-records)))
(car set-of-records))
(else (lookup given-key (cdr set-of-records)))))

We can define simple procedures for making a record out of a key and its data, and for extracting the key and data from an existing record in order to test the procedure above.

(define (key record) (car record))
(define (data record) (cdr record))
(define (make-record key data) (cons key data))

(define database
(list (make-record 1 'Bill)
(make-record 2 'Joe)
(make-record 3 'Frank)
(make-record 4 'John)))

> (lookup 3 database)
'(3 . Frank)
> (data (lookup 1 database))
'Bill

Exercise 2.66 asks us to implement the lookup procedure for the case where the set of records is structured as a binary tree, ordered by the numerical values of the keys.

We can start by including the list->tree and partial-tree procedures given for exercise 2.64, along with a few required supporting procedures.

(define (entry tree) (car tree))
(define (make-tree entry left right)
(list entry left right))

(define (list->tree elements)
(car (partial-tree elements (length elements))))

(define (partial-tree elts n)
(if (= n 0)
(cons '() elts)
(let ((left-size (quotient (- n 1) 2)))
(let ((left-result (partial-tree elts left-size)))
(let ((left-tree (car left-result))
(non-left-elts (cdr left-result))
(right-size (- n (+ left-size 1))))
(let ((this-entry (car non-left-elts))
(right-result (partial-tree (cdr non-left-elts)
right-size)))
(let ((right-tree (car right-result))
(remaining-elts (cdr right-result)))
(cons (make-tree this-entry left-tree right-tree)
remaining-elts))))))))

This makes it easier to convert the existing database to one structured as a binary tree.

> (define tree-db (list->tree database))
> tree-db
'((2 . Joe) ((1 . Bill) () ()) ((3 . Frank) () ((4 . John) () ())))

Finally, we can write the new implementation of lookup using element-of-set? as a guide.

(define (lookup given-key set-of-records)
(cond ((null? set-of-records) #f)
((= given-key (key (car set-of-records)))
(car set-of-records))
((< given-key (key (car set-of-records)))
(lookup given-key (left-branch set-of-records)))
((> given-key (key (car set-of-records)))
(lookup given-key (right-branch set-of-records)))))

> (lookup 3 tree-db)
'(3 . Frank)
> (lookup 1 tree-db)
'(1 . Bill)
> (lookup 5 tree-db)
#f
> (data (lookup 2 tree-db))
'Joe

For links to all of the SICP lecture notes and exercises that I've done so far, see The SICP Challenge.

KAJlogic said...

Hey I have a question about your post on stack overflow. I could not reply because I do not have enough reputation. However, I am trying to work with the email situation. I am confused; I do not have the packages that you labled on there. Were they created by you? What should I read up on to understand the email situation. I have been working with java for a while now so I don't need the basics; I need someone to point me in the right direction.

My email: KAJLogic@gmail.com

Bill the Lizard said...

KAJlogic,

You must be talking about the post How to send an email by Java application using Gmail/ Yahoo/ Hotmail. Everything you need should be in the JavaMail API, which is an optional add-on if you're using Java SE, and is already included if you use Java EE.

KAJlogic said...

BTL,

xuanji said...

Hello,

I'm working on an interactive version of SICP that has an embedded scheme interpreter so users can play with the example code, do exercises etc. I've found your site very helpful for checking my answers to the exercises. The site is available at xuanji.appspot.com/isicp/1-1-elements.html and I'll appreciate if you have any comments on it.

xuanji said...
This comment has been removed by the author.