ITEC 380 Program 5

Due Noon Friday, December 10, 2010
Late submissions will NOT be accepted.

Submit command : submit itec380-01 p5.lsp


Write the following Common LISP functions (approximate point values for each function are given):

   (myLast List)               ; 40 points

   (nextToLast List)           ; 35 points

   (lastTwoReversed List)      ; 15 points

   (reverseLastTwo List)       ; 10 points
(Sound familiar?) The precondition for all these functions is that the argument List is actually a list.

Function myLast evaluates to the last element of List and nextToLast evaluates to the next to last element of List. If either element does not exist, then the function evaluates to nil.

Function lastTwoReversed evaluates to a list containing exactly two elements: the last two elements of List, but in reverse order. It evaluates to nil if List is a list with 0 or 1 elements.

Function reverseLastTwo evaluates to a list that is identical to List, except that the last two elements are reversed. It evaluates to nil if List is a list with 0 or 1 elements.

Here are some examples:

>(myLast '(1 2 3) )
3

>(myLast '() )
nil

>(myLast '(1 2 (3 4) ) )
(3 4)

>(nextToLast '(1 2 3) )
2


>(nextToLast '(1) )
nil

>(lastTwoReversed '(1 2 3) )
(3 2)

>(lastTwoReversed '(1) )
nil

>(reverseLastTwo '(1 2 3 4) )
(1 2 4 3)

>(reverseLastTwo '(1 2) )
(2 1)

>(reverseLastTwo  '(1) )
nil
 
As usual, you are not to use any built in functions that essentially solve the problem for you. (In other words, you can't use the built in function last!) You are also not to simply reverse the list, operate on it, the reverse again. Also as usual, if you need a list function, then should simply write it yourself.

Please be certain to use these exact names. I will not grade your program if your names are not exactly correct. Also, make sure that you do not mix up the names.

Your program should follow good style. In this case this means to use white space to make your program easier to understand (ie easy to match parentheses) and to choose meaningful variable names.

You should also include comments to make the code easier to understand as well as a header comment describing you, the project, etc. In Common Lisp, characters following a semicolon (ie ;) are ignored.

Use /usr/bin/gcl on rucs2 to run Common Lisp. You can use (bye) or control D to exit gcl. You might want to try out the command % in vi to help in writing your lisp code. You may use any system to develop your code, however remember to test it on rucs2 which is where I will grade it.

Put your program in a file called p5.lsp and use the command

submit itec380-01 p5.lsp

to turn it in. Remember that you must be on rucs2 or one of its clients for submit to work correctly.

Last modified: