CS341, Spring 1996
LISP Assignment #1
Assigned 4/5, due 4/15.
1. Some simple recursive functions.
(In some cases these functions,
or ones like them, are defined as part of the CommonLisp language.
Do not use the built-in function in writing your function.)
(add-numbers list), which returns the
sum of all the numbers in its list argument. Non-numbers should
be ignored. Write two versions: one in
which the list is simply a flat list of objects (no nesting), the
other in which the list structure may be nested.
(obj-position object list) which returns
the index of the first occurrence of the object in the list. Assume
that the list is not nested. (Extra credit: extend the function
so it would return a reasonable answer if the list is nested.
First discuss what the function should return, then write
the function that does so.)
(generate-indices num) that takes
as input an integer 0 or greater, and generates the list
(0 1 2 ... num). You should signal an error if the argument
is less than 0. (Hint: two functions may be easier than one.)
(first-half list) and (second-half list)
that do exactly that. Be careful about lists with odd numbers
of elements. You want to assure that the list always splits
into two disjoint sublists.
(binary-search integer sorted-list-of-integers)NIL if the input integer is
not in the list.
merge-sort which takes a single list of
integers as input and returns it sorted. In implementing the sort
you should also write a recursive function my-merge that takes
two sorted lists as input and merges them to produce a single sorted
list. (MERGE is already a function in Common Lisp and the
interpreter complains if you re-define that function. You are to write
your own version of merge; don't use the built-in version.)
Extra credit: make merge-sort work on arbitrarily nested lists of
integers.
2. A Simple ``Bank''
For this problem you will write some functions that will simulate the operation of a simple bank. A bank is basically just a a set of accounts. Each account in turn has a unique account number, a balance, and a personal identification number (PIN). The PIN is required in order to withdraw money. The top-level functionality for the bank will be:
Start by defining data structures for the bank and for accounts. Then you should write some ``support'' functions for doing operations on the bank. Here are some functions you will probably want to write:
(defun bank-verify-account-number (bank account-number)) => boolean (defun bank-verify-pin (bank account-number pin)) => boolean (defun bank-add-account (bank pin) => account-number (defun bank-delete-account (bank account-number) => void (defun bank-change-pin (bank account-number pin)) => void (defun bank-deposit (bank account-number amount) => void (defun bank-withdraw (bank account-number amount) => voidNote that these functions will do no error checking, nor will they verify a PIN number. They are low-level operations on the
bank data structure.
Next you will implement a simple command loop. It will prompt you for
an operation (create account, deposit, withdraw, etc.), prompt you for
an account number and PIN as appropriate, and do all the error checking
as appropriate: does this account exist? does the PIN match? is there
enough money in the account? It then calls the appropriate bank-
function to do the transaction, and prints an error or verification message.
The main function you will write is (process-commands bank)---note
that you have to create a bank structure ahead of time.
Code available to you:
~c341/code/psl1/bank.fasl. You can load this file, but the
Lisp source code is not available. To run the demo you might do
the following:
(load "~c341/code/psl1/menus")
(load "~c341/code/psl1/bank")
(process-commands (make-bank :name "My bank"))
~c341/code/psl1/menus.lispThe main helper function takes a list of prompt strings as input, puts up the menu, and returns a number that is the index of the string the user selects. There is also some code to input numbers and to input and verify account numbers and PINS. strings. You must use this code in implementing your solution.