Banner

Chen, Michael Chungkun

03-21-2002

CS131-01

Project 2

 

Cute Database

 

 

User Documentation

 

Welcome to the CuteDB documentation, the simple database system written exclusively in Scheme.  The database is designed to contain records of simple publications, able to store the publication title, publication year, author name, and price.  CuteDB uses the publication title as the key index field, which means that it is invalid to have two records with the same publication titles.  This database system begins with a default database, which includes 10 records.  CuteDB allows 4 different kinds of simple queries, and also the insertion and deletion of records.  The four query types include: by author, by year, by both author and year, and by cost limit.

 

In order to run the program, first load up the cutedb.scm file into DrScheme, or similar Scheme interpreters.  For DrScheme, make sure that the language is set to Full Scheme in order enable the program to run correctly.

 

Once we load up the source, we can simply click on execute and the program will begin to run automatically.  We should be able to see a screen similar to the screen on the right.

 

 

 

 

 

At this point, we may issue any database query, or other commands.  For a list of available commands, you may type “cmd” at the “DB:>” prompt at any time.

 

 

We will now discuss each command in detail.

 

 

 


 

 

REC command:

The rec command simply displays all of the records that are currently stored in the database.  It will display each record on one line.  The first item in the record is the title of the publication.  The second item in the record is the year of publication, the next item is the author of the publication, and the last item contains the price.

 

 

 

 

 

Query 1:

The first type of query is to query by the year of publication.  In order to execute queries of this kind, the command is “q1”.

 

The query would return all records that match the specified criteria, if none were found, it would display “No results found!”

 

 

 

 

Query 2:

The second type of query is to query by the author’s name.  Simply type in “q2” at the database prompt, and then enter the author’s name as a list when prompted.  This will then return all results that matched.

 

 

 

 

 

 

Query 3:

This query is basically a combination query.  It allows us to queries for records with a specified author, published in a specified year.  Once again the author’s name must be entered as a list.

 

 

 

 

Query 4:

The last kind of query that is allowed is a query to find all publications that cost strictly less than a specified cost.  This will return all records that contain publications that are less than whatever is specified.  Publications with the same cost will not be displayed.


 

Inserting:

In order to insert new records into the current database, the “insert” command is provided.  The “insert” command maybe used in different ways, experts may elect to use the command line method, where we include the publication title, publication year, author, and price on the same line as the insert.  (Refer to figure on left)

Others who wish to use a more interactive method may just type “insert” at the database prompt and supply the information when prompted. (Refer to figure on right)  Author and publication title must be entered as lists.



After insertion, the program will return an OK, or FAILED message, which informs us if the insert operation succeeded.  In either case, the current database after the operation will be displayed.

 

Deleting:

In order to delete records that may no longer be necessary or useful, we have the “delete” command.  In order to use it, simply enter “delete” at the database prompt for the interactive method as shown on the right.  The command line method is also available using this command and requires only the publication title as a parameter as shown on the left.  Afterwards the result of the operation is returned, either OK, or Failed.  And finally the database after the operation is displayed.  Remember the title must be in the form of a list.


 


 

Quit:

Once we are done with the program, we may elect to quit the cuteDB program by typing the command “quit” at the database prompt.  CAUTION!  All changes made to database during the execution will be lost once you quit.


 

System Documentation

 

When starting the program the user runs the (cuteDB) function.  This function basically displays a simple, small welcome screen and then executes the interaction loop.  This is included in the definition file, which causes the program to run automatically upon executing.

 

The interaction loop named (PromptCycle) is a simple function yet is a fundemental part of the program.  It begins by displaying a prompt, then reads the input from the user.  After obtaining input, it determines the symbol that the user typed, and directs the program flow based on that.

The following is a list of commands and the behavior of the program based on the command.

quit:  the interaction loop displays a goodbye message, and stops running.

rec:   the function printDB with the entire database as a parameter.

cmd:   the function runs the showMenu function which displays the program menu.

q1:    the function performs a type 1 query.

q2:    the function performs a type 2 query.

q3:    the function performs a type 3 query.

q4:    the function performs a type 4 query.

insert:       the function runs the insert fuction.

delete:       the function runs the delete function.

Any other key sequences will result in an invalid command message except for the Easter Egg command. [Find it if you can J]

 

The (showMenu) function basically is a sequence of displays, and newlines.  It displays the menu to the user whenever it is executed.

 

The (Query-Year) function queries the database by the year entered by the user.  This function uses the more efficient map, instead of recursively querying through the database.  And an internal lambda is used which returns records that match with the specified year.  Apply append is used to remove nulls from the list, which is why every record is wrapped in an extra level of list.  Psuedocode:

    ;Displays a prompt for year

    ;Reads in year and stores as year

    ;Error Check data for type

    ;Applys the append function to a list of lists of records

       ;This will remove null records from the result

       ;Map the lambda function with one parameter to each

       ;Record of the database this function matches the year

           ;if the Record year matches our year query

           ;Return a list of the record

           ;Otherwise return null also removed by append

       ;Function is applied to the DataBase list

    ;If the result was null then we had no matches

    ;Print the result

 

The (Query-Author) function queries the database by the author entered by the user.  This function also uses the efficient map method.  Basically the core structure of the code is the same as the previous (Query-Year) function, except the parts concerning the year are changed to the respective parts concerning the author.  Psuedocode:

    ;Prompt

    ;Read input and set to author

    ;If author type is wrong print error message

        ;apply append to remove nulls

          ;map the lambda that matches author to database

            ;If we find a match

               ;Return a list of the record

               ;Otherwise return a null

          ;Map to database

    ;If no results display no result message

    ;Prints the results if any


The (Query-Author-Year) function queries the database by both author and year of the publication.  This function is basically a combination of the previous two functions.  It also uses the map method.  Psuedocode:

    ;Prompt name

    ;Read name set as author

    ;Check the type of author

    ;Prompt for publication year

    ;Read in year

    ;Check the type of year

    ;Apply append to get rid of nulls

         ;Map the lambda function to match against year and author

           ;If the author matches and the year also matches

              ;return the list of record

              ;Else return null

         ;Apply lambda to DataBase         

    ;If the result were empty display no result message

        ;Print whatever results came back

 

The (Query-Price) function queries the database by a maximum price.  It displays publications that cost less than the specified price.  Equal cost items are not displayed.  This query also make use of the map method, the lambda function basically determines if the record costs less than a specified cost.  Psuedocode:

    ;Prompts for price

    ;Reads in cost

    ;Checks type of cost

    ;Apply append to get rid of nulls

       ;Map the lambda to find record less than cost

            ;If record has a cost less than one specified

                ;Return a list of the record

                ;Else return null

       ;maps to database

    ;If the result was empty display no result message

    ;Displays whatever results were returned

 

The (insert) function inserts new records into the database after it makes sure the records are in the valid format, and there are no duplicates.  The function resets the database to equal the old database appended with the new record.  Psuedocode:

      ;Display prompt if data was not already entered

      ;Gets the title from input

      ;Display prompt if data was not already entered

      ;Gets year from input

      ;Display prompt if data was not already entered

      ;Gets author from input

      ;Display prompt if data was not already entered

      ;Gets price from input

 

      ;Sets up the record   

      ;Calls the (CheckRecord) function which checks if the record is allowed to be

               entered into database

         ;If true Enter record into database and display ok

         ;Else display failed

      ;Displays the current database

 

The (delete) function deletes unwanted records from the database after it makes sure the record exists.  Psuedocode:

     ;Display prompt if data was not already entered

     ;Reads in title from input    

     ;Checks to make sure title is a valid list

     ;Search in database for a record with the title by calling (SearchRecord)

     ;If the search returned true

        ;It was found, so we set the Global Database

               ;Equal to the result of Apply append, which gets rid of nulls, to

                     ;Maps the lambda function to return or not return a record

                           ;If the record matches in title

                                ;returns null

                                ;Else returns the list of the record

                     ;Maps to DataBase

           ;Display the ok message

        ;else record not found Display the failed message

     ;Displays the current database


The (CheckRecord) function basically does simple type checking against the 4 elements of the record.  If all four elements are the right type, it continues to check if the record title has already been used in the database.  By calling (SearchRecord) with the global database, and the title in the new record as the parameters.  This function returns true if the record isn’t found, and false if it is.

 

The (SearchRecord) function simply takes in a database and a title as it’s input.  It recursively searches the database for a record with the same title, and returns true if the title is found, false otherwise.  Psuedocode:

    ;Not found, return false

    ;If exists in database return true

    ;Recursively Check the other Records

 

The (printDB) function simply prints the database that is passed to it, recursively.  First it prints the first element, with the (printREC) function; then it calls itself to print the rest of the database.

 

The (printREC) function prints the record with the pretty-print capitalizing capability.

    ;Error Checking

    ;Check for null, is so it’s an error.  

    ;Size Checking Makes sure record has 4 elements no more no less.

   

    ;Displays the record

          ;First item is capitalized with the (displayUPPER) function

          ;Second item is normal

          ;Third item is capitalized with the (displayUPPER) function

          ;Fourth item is normal

 

The (displayUPPER) function takes in a list as the parameter and then capitalizes the first letter of each element in the list by converting it into a string, getting the first character, capitalizing that, and then appending it to the string version of the rest of the element.

 

    ;List is empty return null

    ;Otherwise

        ;display the concatenation of two strings

            ;Converts to uppercase the first character of the

                  ;symbol converted into a string

            ;Returns the rest of the symbol converted into a string        

        ;If there is more items in this list, display a space

        ;Recursively displays the rest of this list.

 

All the words are defined globally to equal their symbolic counterpart.  It doesn’t make the program any better or worse for doing this, I just felt like doing this.  A sample definition with the word “The”

    (define The 'The)

 

The (DataBase) is defined as a list of records.  Records are a list of Title, Year, Author, and Cost.  Title, and Author are both list of symbols.  The following is a example definition of a database with one record.

 (define DataBase

  (list

   (list (list The Warrior) 1997 (list Spinoza) 120)

   );end list

);end define

 

The old database definition where the Title and Author were quoted strings were also kept, the old database is named (oldDataBase).

 

Both recursive, and map methods were used throughout this program to demonstrate the capability to use either.  I much prefer the map method because it appears intuitively to be more efficient, the function doesn’t execute recursively so it doesn’t need stack space to maintain local variables and return addresses.


This program is case insensitive.  Therefore when matching the author “Plato” we can easily type in “plato” at the prompt and still obtain correct results.  Although the database stores everything in lower case format, my database program internally converts the first letter of each item in the lists that represents titles and author names to uppercase letters whenever it is displayed to the user.  Thus providing a much nicer looking and easier to read screen.

 

My insert and delete function are very flexible.  On can enter all the required parameters on the command line, or not enter any and let the program prompt them.  The insert command can even handle receiving some of the parameters, and it will prompt for the rest.  For example, look at the figure on the right.

 

Only two of the elements were supplied, so the program prompted for the other two elements.

 

Also note that instead of a easily confused “>” prompt, I opted to display a more descriptive “DB:>” prompt.  This allows the user to further differentiate between Scheme prompts, and CuteDB prompts.

 

As it is evident, the extra credit features for inserting records and deleting records were both implemented.

 

The system requires users to enter titles and author names in the form of a list to allow multiple words for titles and author names.  An earlier implementation of title and author names as strings was also done, but it was converted into a list implementation after the specification changed.
Source Code


;Chen, Michael Chungkun

;03-21-2002

;CS131-01

;Project 2

;002-764-157

 

;Global Database Definition

 

;Define all the words used as symbols.

(define The 'The) (define Warrior 'Warrior) (define Spinoza 'Spinoza)

(define Quest 'Quest) (define Joe 'Joe) (define Davis 'Davis)

(define Laughable 'Laughable) (define Loves 'Loves)

(define Unbearable 'Unbearable) (define Lightness 'Lightness)

(define Can 'Can) (define Williams 'Williams) (define Ethics 'Ethics)

(define Jennifer 'Jennifer) (define Lee 'Lee) (define Human 'Human)

(define Understanding 'Understanding) (define Sarah 'Sarah)

(define Dina 'Dina) (define Superman 'Superman)

(define Kathryn 'Kathryn) (define Standing 'Standing)

(define Still 'Still) (define David 'David) (define Hume 'Hume)

(define StarTrek 'StarTrek) (define Nobody 'Nobody)

(define Plato 'Plato)

 

;Actual Definition of DataBase

(define DataBase

  (list

   (list (list The Warrior) 1997 (list Spinoza) 120)

   (list (list The Quest) 1999 (list Joe Davis) 45)

   (list (list Laughable Loves) 1997 (list Joe Davis) 89)

   (list (list The Unbearable Lightness) 1982 (list Can Williams) 67)

   (list (list The Ethics) 1976 (list Jennifer Lee) 145)

   (list (list Human Understanding) 1666 (list Sarah Dina) 34)

   (list (list Superman) 1975 (list Kathryn Lee) 178)

   (list (list Standing Still) 1912 (list David Hume) 165)

   (list (list StarTrek) 1982 (list Can Williams) 46)

   (list (list Nobody) 1666 (list Plato) 12)

   );end list

);end define

 

;Definition of old Database when quotes were used

(define oldDataBase

  (list

   (list "The Warrior" 1997 "Spinoza" 120)

   (list "The Quest" 1999 "Joe Davis" 45)

   (list "Laughable Loves" 1997 "Joe Davis" 89)

   (list "The Unbearable Lightness" 1982 "Can Williams" 67)

   (list "The Ethics" 1976 "Jennifer Lee" 145)

   (list "Human Understanding" 1666 "Sarah Dina" 34)

   (list "Superman" 1975 "Kathryn Lee" 178)

   (list "Standing Still" 1912 "David Hume" 165)

   (list "StarTrek" 1875 "Berkeley" 46)

   (list "Nobody" 1666 "Plato" 12)

   );end list

  );end Define

 

;**********************************************************

;****       Main loader function                       ****

;**********************************************************

(define (cuteDB)

  (begin

    (display "Welcome to cuteDB")

    (newline)

    (display "By: Michael Chungkun Chen")

    (newline)

    (display "SID: 002764157")

    (newline)

 

    (PromptCycle) ;Begin the Interaction loop

   

    );end begin

  )

 

;**********************************************************

;****       Main User Interaction Cycle                ****

;**********************************************************

(define (PromptCycle)

  (begin

    (newline)

    ;Displays the prompt

    (display "DB:>")

   

    ;reads user input and stores as variable command

    (let ((command (read)))

      (cond

        ((eq? command 'quit)

         ;Quits program

         (display "Thank you for using my cuteDB"))

        ((eq? command 'rec)

         ;Displays all the Records

         (begin (printDB DataBase)

                (PromptCycle)))

        ((eq? command 'cmd)

         ;Display the menu

         (begin (showMenu)

                (PromptCycle)))       

        ((eq? command 'q1)

         ;Year query

         (begin (Query-Year)

                (PromptCycle)))

        ((eq? command 'q2)

         ;Author query

         (begin (Query-Author)

                (PromptCycle)))

        ((eq? command 'q3)

         ;Author and year query

         (begin (Query-Author-Year)

                (PromptCycle)))

        ((eq? command 'q4)

         ;Cost query

         (begin (Query-Cost)

                (PromptCycle)))

        ((eq? command 'insert)

         ;Insert Record into Database

         (begin (insert)

                (PromptCycle)))

        ((eq? command 'delete)

         ;Delete Record from Database

         (begin (delete)

                (PromptCycle)))

        ((eq? command 'easter)

         ;Pretty print [This is an easter egg for you to find]

         (begin (printDBp DataBase)

                (PromptCycle)))

         (#t

          ;Invalid command given, display message and reprompt

          (begin (display "Invalid command.  Type in cmd for a list of commands.")

                 (PromptCycle)))         

         );end cond

      );end let

    );end begin

  );end driver function

 

;**********************************************************

;****       Shows the Main Menu                        ****

;**********************************************************

(define (showMenu)

  ;Just simple display and newlines to display a menu.

  (begin

    (display "q1:       find the books published in x year.")

    (newline)           

    (display "q2:       find the books written by x author.")

    (newline)

    (display "q3:       find the books written by x author, published in y year.")

    (newline)

    (display "q4:       find the books cheaper than x price.")

    (newline)

    (display "rec:     show all current records in the database.")

    (newline)

    (display "insert:  inserts a new record into database")

    (newline)

    (display "         insert publication_name year author price")

    (newline)

    (display "         insert *** This will prompt user for more information ***")

    (newline)

    (display "delete:  deletes record with given publication name")

    (newline)

    (display "         delete publication_name")

    (newline)

    (display "         delete  *** This will prompt for publication name ***")

    (newline)

    (display "cmd:     show all available commands")

    (newline)

    (display "quit:    quit the program")

    (newline)   

    );End Begin

  );End Define menu

 

;**********************************************************

;****      Query by year                               ****

;**********************************************************

(define (Query-Year)

  ;Local variables

  (let ((year null)

        (result null))

   

    ;Displays a prompt for year

    (begin (display "Enter publication year: ")

           ;Reads in year and stores as year

           (set! year (read))

           ;Error Check data for type

           (if (not (integer? year))

               ;Display Error Message

               (begin (display "Enter a integer next time!")

                      (newline)))

          

           ;Sets the result equal to the return of the following

           (set! result

                 ;Applys the append function to a list of lists of records

                 ;This will remove null records from the result

                 (apply append

                        ;Map the lambda function with one parameter to

                        ;each record of the database this function

                        ;matches the year

                        (map (lambda (RECORD)

                               ;if the Record year matches our year query

                               (if (eq? (cadr RECORD) year)

                                   ;Return a list of the record

                                   ;One level of list is removed by append

                                   (list RECORD)

                                   ;Otherwise return null also removed

                                   ;by append

                                   null))

                             ;Function is applied to the DataBase list

                             DataBase)))

          

           ;If the result was null then we had no matches

           (if (null? result) (display "No results found!"))

           (newline)

           ;Print the result

           (printDB result)

           );End begin

    );End let

  );End define

 

;**********************************************************

;****      Query by author                             ****

;**********************************************************

(define (Query-Author)

  ;Local variables

  (let ((author null)

        (result null))

    ;Prompt

    (begin (display "Enter author name (as list): ")

           ;Read input and set to author

           (set! author (read))

           ;If author type is wrong print error message

           (if (not (list? author))

               (begin (display "Remember to enter author as a list!")

                      (newline)))

           ;Set result to equal the return of...

           (set! result

                 ;apply append to remove nulls

                 (apply append

                        ;map the lambda that matches author to database

                        (map (lambda (RECORD)

                               ;If we find a match

                               (if (equal? (caddr RECORD) author)

                                   ;Return a list of the record

                                   (list RECORD)

                                   ;Otherwise return a null

                                   null));End lambda

                             ;Map to database

                             DataBase)))

          

           ;If no results display no result message

           (if (null? result) (display "No results found!"))

           (newline)

           ;Prints the results if any

           (printDB result)

           );End begin

    );End let

  );End define

 

;**********************************************************

;****      Query by author and year                    ****

;**********************************************************

(define (Query-Author-Year)

  ;Local variables

  (let ((year null)

        (author null)

        (result null))

    ;Prompt name

    (begin (display "Enter author name (as list): ")

           ;Read name set as author

           (set! author (read))

           ;Check the type of author

           (if (not (list? author))

               ;Displays a error message if it is not a list

               (begin (display "Remember to enter Author name as a list!")

                      (newline)))

           ;Prompt for publication year

           (display "Enter publication year: ")

           ;Read in year

           (set! year (read))

           ;Check the type of year

           (if (not (integer? year))

               ;Displays an error message if it is not a integer

               (begin (display "Enter a integer next time!")

                      (newline)))

          

           ;Sets the result to equal the return of...

           (set! result

                 ;Apply append to get rid of nulls

                 (apply append

                        ;Map the lambda function to match against

                        ;year and author

                        (map (lambda (RECORD)

                               ;If the author matches and

                               (if (and (equal? (caddr RECORD) author)

                                        ;the year also matches

                                        (eq? (cadr RECORD) year))

                                   ;return the list of record

                                   (list RECORD)

                                   ;Else return null

                                   null))

                             ;Apply lambda to DataBase

                             DataBase)))

          

           ;If the result were empty display no result message

           (if (null? result) (display "No results found!"))

           (newline)

           ;Print whatever results came back

           (printDB result)

           );End begin

    );End let

  );End define

 

;**********************************************************

;****      Query by price                              ****

;**********************************************************

(define (Query-Cost)

  ;Local variables

  (let ((cost null)

        (result null))

    ;Prompts for price

    (begin (display "Enter price: ")

           ;Reads in cost

           (set! cost (read))

           ;Checks type of cost

           (if (not (number? cost))

               ;If not number then display error message

               (begin (display "Enter a number next time!")

                      (newline)))

          

           ;sets the result equal to...

           (set! result

                 ;Apply append to get rid of nulls

                 (apply append

                        ;Map the lambda to find record less than cost

                        (map (lambda (RECORD)

                               ;If record has a cost less than

                               ;one specified

                               (if (< (cadddr RECORD) cost)

                                   ;Return a list of the record

                                   (list RECORD)

                                   ;Else return null

                                   null))

                             ;maps to database

                             DataBase)))

          

           ;If the result was empty display no result message

           (if (null? result) (display "No results found!"))

           (newline)

           ;Displays whatever results were returned

           (printDB result)

           );End begin

    );End let

  );End define

 

;**********************************************************

;****       Inserting procedure                        ****

;**********************************************************

(define (insert)

  ;Sets up local variables

  (let ((title null)

        (year null)

        (author null)

        (price null)

        (RECORD null))

    (begin

      ;Display prompt if data was not already entered

      (if (eq? (peek-char) #\newline)

          (display "Publication name (enter as list): "))

      ;Gets the title from input

      (set! title (read))

      ;Display prompt if data was not already entered

      (if (eq? (peek-char) #\newline)

          (display "Year: "))

      ;Gets year from input

      (set! year (read))

      ;Display prompt if data was not already entered

      (if (eq? (peek-char) #\newline)

          (display "Author (enter as list): "))

      ;Gets author from input

      (set! author (read))

      ;Display prompt if data was not already entered

      (if (eq? (peek-char) #\newline)      

          (display "Price: "))

      ;Gets price from input

      (set! price (read))

     

      ;Sets up the record

      (set! RECORD (list title year author price))

     

      ;Calls the check record function which checks if

      ;the record is allowed to be entered into database

      (if (CheckRecord RECORD)

          ;CheckRecord returns true

          (begin

            ;Enter record into database

            (set! DataBase (append DataBase (list RECORD)))

            ;Display ok

            (Display "OK")

            )

          ;Else display failed

          (Display "Failed"))

      (newline)

      ;Displays the current database

      (printDB DataBase)

      );End begin

    );End let

  );End define

 

;**********************************************************

;****       Deleting procedure                         ****

;**********************************************************

(define (delete)

  ;Set up the local variables

  (let ((title null))

    (begin

      ;Display prompt if data was not already entered

      (if (eq? (peek-char) #\newline)

          (display "Publication name (enter as list): "))

      ;Reads in title from input

      (set! title (read))

     

      ;Checks to make sure title is a valid list

      (if (not (list? title))

          ;Display error message if not

          (begin (display "Enter a Publication name as a list next time!")

                 (newline)))

      ;Search in database for a record with the title

      (if (SearchRecord DataBase title)

          ;It was found, so we set the Global Database

          (begin (set! DataBase

                       ;Equal to the result of

                       ;Apply append which gets rid of nulls

                       (apply append

                              ;Maps the lambda function to return

                              ;or not return a record

                              (map (lambda (REC)

                                     ;If the record matches in title

                                     (if (equal? (car REC) title)

                                         ;returns null

                                         null

                                         ;Else returns the list

                                         ;of the record

                                         (list REC)))

                                   ;Maps to DataBase

                                   DataBase)))

                 ;Display the ok message

                 (display "OK"))

          ;else record not found Display the failed message

          (display "Failed"))

      (newline)

      ;Displays the current database

      (printDB DataBase)

      );End begin

    );End let

  );End define

 

;**********************************************************

;****       Checks Record for validity                 ****

;**********************************************************

(define (CheckRecord REC)

  (cond

    ;Error Checking

    ;Not empty record

    ((null? REC) #f)

    ;First item is a list [Title]

    ((not (list? (car REC))) #f)

    ;Third item is a list [Author]

    ((not (list? (caddr REC))) #f)

    ;Second item is a integer [year]

    ((not (integer? (cadr REC))) #f)

    ;Fourth number is a number [price]

    ((not (number? (cadddr REC))) #f)

    ;Tries to find record in database

    ;ok to insert if not found

    (#t (not (SearchRecord DataBase (car REC))))

   );end cond

  );end function

 

;**********************************************************

;****     Searches for Record returns true if found    ****

;**********************************************************

(define (SearchRecord DB name)

  (cond

    ;Not found, return false

    ((null? DB) #f)

    ;If exists in database return true

    ((equal? name (caar DB)) #t)

    ;Recursively Check the other Records

    (#t (SearchRecord (cdr DB) name))

   );end cond

  );end function

 

;**********************************************************

;****  Prints a the portion of database passed as DB   ****

;**********************************************************

(define (printDB DB)

  (cond

    ((null? DB) null)

    ;Print first record and recursively print the rest

    (#t (begin (printREC (car DB))

               (printDB (cdr DB))

               ))

    ))

 

;**********************************************************

;****       Prints a single Record as record           ****

;**********************************************************

(define (printREC REC)

  (cond

    ;Error Checking

    ((null? REC) (display "Error No Record"))

   

    ;Size Checking

    ((not (list? REC)) (display "Error, invalid Record entry"))

    ((null? (cdr REC)) (display "Error, invalid Record size<2"))

    ((null? (cddr REC)) (display "Error, invalid Record size<3"))

    ((null? (cdddr REC)) (display "Error, invalid Record size<4"))

    ((not (null? (cddddr REC))) (display "Error, invalid Record size>4"))

    ;End of Size Checking

   

    ;Displays the record

    (#t (begin

          ;The Following selectively forces capitalized words

          ;First item is capitalized

          (display "(") (display #\() (displayUPPER (car REC))

          (display #\))             

          (display " ") (display (cadr REC)) (display " ")

          ;Third item is capitalized

          (display #\() (displayUPPER (caddr REC))

          (display #\))

          (display " ") (display (cadddr REC)) (display ")")

          (newline)

          );end begin

     );end #t cond

   );end cond

  );end function

 

;**********************************************************

;****       Prints a list with capitalized 1st chars   ****

;**********************************************************

(define (displayUPPER list)

  (cond

    ;List is empty return null

    ((null? list) null)

    ;Otherwise

    (#t (begin

          ;display the concatenation of two strings

          (display (string-append (string (char-upcase

                                  ;Converts to uppercase

                                           ;returns the first character

                                           ;of the symbol converted into

                                           ;a string

                                           (string-ref (symbol->string

                                                          (car list))

                                                       0)))

                                  ;Returns the rest of the symbol

                                  ;converted into a string

                                  (substring (symbol->string (car list))

                                             1

                                             (string-length

                                                (symbol->string

                                                    (car list))))))

         

          ;If there is more items in this list, display a space

          (if (not (null? (cdr list))) (display " "))

          ;Recursively displays the rest of this list.

          (displayUPPER (cdr list))

          ))

   )

  )

 

;**********************************************************

;****       Prints a single Record descriptively       ****

;**********************************************************

(define (printRECd REC)

  (cond

    ;Error Checking

    ((null? REC) (display "Error No Record"))

   

    ;Size checking

    ((not (list? REC)) (display "Error, invalid Record entry"))

    ((null? (cdr REC)) (display "Error, invalid Record size<2"))

    ((null? (cddr REC)) (display "Error, invalid Record size<3"))

    ((null? (cdddr REC)) (display "Error, invalid Record size<4"))

    ((not (null? (cddddr REC))) (display "Error, invalid Record size>4"))

    ;End of size checking

   

    ;Displays the record

    (#t (begin (display "Title: ")

               (displayUPPER (car REC))

               (newline)

               (display "Year: ")

               (display (cadr REC))

               (newline)

               (display "Author: ")

               (displayUPPER (caddr REC))

               (newline)

               (display "Price: ")

               (display (cadddr REC))

               (newline)

          );end begin

     );end #t cond

   );end cond

  );end function

 

;**********************************************************

;****  Prints DB in a formated manner                  ****

;**********************************************************

(define (printDBp DB)

  (cond

    ((null? DB) null)

    ;Print first record and recursively print the rest

    (#t (begin (printRECd (car DB))

               (newline)

               (printDBp (cdr DB))

               ))

    ))

 

(cuteDB)

 

 

 


Home | About Me | Text Depository | Future Enhancements | Guest Book | Links

Copyright © 1998-2008 Michael Chungkun Chen
All Rights Reserved.