SICP 2.4 exercises cont. 2

2.74

Full code listing below for all 4 parts. The annoying thing about this problem was I needed to implement two different representation of the files and records. I just did something simple, where the name-information pairs in the records are represented by cons in the alpha division and list in the beta division.

a) Individual division’s file need to have their division name tagged to the front of the records so that the generic get-record procedure can identify which specific get-record procedure to use.

b) Each record should be tagged with the division name in front of the name and the information pairs.

c) Can be implemented in terms of the procedures in a) and b)

d) When a new division is added, all it’s records and files need to be tagged. Functions need to be defined for getting the salary from records and the records from files and then put into the table.

My procedures were a little bit hackish in that the individual get-record and get-salary procedures need to deal with the tag in the records. This is invisible to the user of the generic procedures however.

(define (make-record-beta name . pairs)
  (define (make-info kv-pair)
    (if (null? kv-pair)
        '()
        (cons (list (car kv-pair) (cadr kv-pair)) (make-info (cddr kv-pair)))))
  (list name (make-info pairs)))

(define (get-record-beta name file)
  (cond ((null? file) #f)
        ((eq? (caar file) name) (car file))
        (else (get-record-beta name (cdr file)))))

(define (get-salary-beta record)
  (define (retrieve pairs)
    (if (eq? (caar pairs) 'salary)
        (cadar pairs)
        (retrieve (cdr pairs))))
  (retrieve (cadr record)))

(define (get-tag tagged-file)
  (car tagged-file))

(define (contents tagged-file)
  (cdr tagged-file))

(define (tag-file tag file)
  (cons tag file))

(define (make-record-alpha name . pairs)
  (define (make-info kv-pair)
    (if (null? kv-pair)
        '()
      (cons (cons (car kv-pair) (cadr kv-pair))
            (make-info (cddr kv-pair)))))
  (tag-file 'alpha (cons name (make-info pairs))))

(define (make-file-alpha . records)
  (tag-file 'alpha records))

(define (make-file-beta . records)
  (tag-file 'beta records))

(define (install-division-alpha)
  (define (get-record-alpha name file)
    (cond ((null? file) #f)
          ((eq? name (cadar file)) (car file))
          (else (get-record-alpha name (cdr file)))))

  (define (get-salary-alpha record)
    (define (retrieve pairs)
      (if (eq? (caar pairs) 'salary)
          (cdar pairs)
        (retrieve (cdr pairs))))
    (retrieve (cdr record)))
  (put 'get-record 'alpha get-record-alpha)
  (put 'get-salary 'alpha get-salary-alpha))

(define (install-division-beta)
  (define (get-record name file)
    (cond ((null? file) #f)
          ((eq? name (cadar file)) (car file))
          (else (get-record name (cdr file)))))
  (define (get-salary record)
    (define (retrieve pairs)
      (if (eq? 'salary (car pairs))
          (cadr pairs)
          (retrieve (cddr pairs))))
    (retrieve (cdr record)))
  (put 'get-record 'beta get-record)
  (put 'get-salary 'beta get-salary))

(define (get-record name file)
  ((get 'get-record (get-tag file)) name (contents file)))

(define (get-salary record)
  ((get 'get-salary (get-tag record)) (contents record)))

(define (find-employee-record name files)
  (if (null? files)
      #f
    (let ((result (get-record name (car files))))
      (if result result
        (find-employee-record name (cdr files))))))

Comments: Skipped two days, which is too bad, Despite there being so few problems in this chapter, I seem to be taking a very long time. I suppose that is what happens when there are other obligations to meet.

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s