CS 22

Clab 8: Using lists to store a database


If you have any questions from last clab, or need help finishing up that material, begin with it and get your questions answered.

Now consider building a database in the following way. Information on each individual (called a record) consists of a list of lists. Each sublist for an individual will be a list that consists of a property-value pair. All the information for one individual will be a list of property-value lists for that person. The entire database will be a list of the lists for each individual. For example, evaluate:

(define apers '( (acno 2146) (name jones) (bal 36.00))) ;one record (define bpers '((name gold) (acno 2984) (bal 98.00) (adrs (4 Univ ave)))) (define cpers '( (acno 3095) (bal 140.00))) (define dbas (list apers bpers cpers)) ;the whole data base.

Evaluate dbas. Try taking the car, cdr, caar, cadar, cadr of dbas. If you haven't read about c...r from online help before, look up cadr now and make sure you understand what you get as a result of the above evaluations. Of course, we are here to answer questions, so don't hesitate to ask.

Define and debug a function valofpr that satisfies the following specification:

; rec is assumed to be a record as defined ; above. prop is the name of a property. ; valofpr returns the value of the property ; with property name prop. If rec does not ; have such a property, valofpr returns '(). (define (valofpr rec prop) ... For example: (valofpr bpers 'acno) should return 2984. You might ask yourself why acno is quoted but bpers is not. Now, use valofpr to help define and debug a function find that satisfies the following specification: ; find searches the data base bound to db for ; a record whose property prop has ; value val. If such a record is found, ; find returns the whole record else '(). (define (find db prop val) ... For example: (find dbas 'bal 140.) returns ((acno 3095) (bal 140.)) and (find dbas 'name 'jones) returns ((acno 2146) (name jones) (bal 36.)).

Finally, use both of the above to help define and debug a function balfromacno that satisfies the following specification:

;;; balfromacno searches the database bound to db for a record ;;; with an account number of num. If the search is successful ;;; and if that record has ;;; a property-value sublist with property bal then ;;; the associated value is ;;; returned. If the either the search fails ;;; or there is no property bal, ;;; the empty list is returned. (define (balfromacno db num) .... For example: : (balfromacno dbas 2146) 36. : (balfromacno dbas 2984) 98. : (balfromacno dbas 3095) 140. : (balfromacno dbas 216) () Of course, you could write many other database retrieval functions in a manner similar to balfromacno. Try some if you have the time. You might think about how truly flexible this database is.

ask any questions you may have.