Assignment
One: Becoming Familiar with lisp
Professor
Heffernan
Handed Out: Sept
5.
Due: Sept, 11 at 6
PM at the beginning of class.
To get started writing lisp code.
To spend about 10-15 hours learning lisp by reading and doing programming exercises.
Note: If you already know lisp, then simply hand in answers to these questions. If you don’t know lisp, which is what I am expecting, please begin learning some lisp.
I suggest working through at your own pace through the ELM-ART Lisp Tutor at
http://www.psychologie.uni-trier.de:8000/elmart
but you are free to learn lisp in any way you care to. There are many good other good books.
1) Please hand in class printer version of the following list functions needed for each of these problems.
2)
Please hand in by email a test copy of the same file.
I will evaluate your text file into a
lisp interpreter and then run these function against some undisclosed test
cases.
3) computer answers to the following question.
Define a function AREA-OF-SQUARE which takes as its argument the side length of a square and calculates the area of a square from these.
Examples:
(AREA-OF-SQUARE 3)
9
(AREA-OF-SQUARE 4)
16
(AREA-OF-SQUARE 0)
0
Define a function SHOPPING that extracts only the item to be purchased from a list containing 2 elements and their quantities. Return the results as a list.
Examples
(SHOPPING '((COFFEE 100 GRAMMES) (BREAD 750 GRAMMES)))
(COFFEE BREAD)
(SHOPPING '((MILK 1000 GRAMMES) (SUGAR 1000 GRAMMES)))
(MILK SUGAR)
Define a predicate, PALINDROME-P, that takes a list as its argument and tests whether this list contains a palindrome i.e. The order of the list elements is the same if read from the left or the right.
Examples:
(PALINDROME-P '(A B B A))
T
(PALINDROME-P NIL)
T
(PALINDROME-P '(A B C))
NIL
Write a function NOT-NUMBERP , that takes one argument and evaluates to T when the argument is not a number and otherwise evaluates to NIL.
Examples:
(NOT-NUMBERP 7)
NIL
(NOT-NUMBERP 'A)
T
(NOT-NUMBERP '(7 8))
T
Define the function INSERTS. Its arguments should be an atom and a list. If the atom is not already contained in the list, it should be inserted into the front of the list. If it is already in the list, the list itself should be returned.
Examples:
(INSERTS 'A '(B C D))
(A B C D)
(INSERTS 'A '(A B C D))
(A B C D)
Define a recursive predicate MONOTONIC-INCREASING-P, that has a number list as its argument. That predicate should test whether the numbers in the number list are in monotonic increasing order. This being the case, T is returned, otherwise NIL.
Examples:
(MONOTONIC-INCREASING-P '(1 2 5 7 9))
T
(MONOTONIC-INCREASING-P '(1 2 5 5 9 9))
T
(MONOTONIC-INCREASING-P '(1 2 5 3 9))
NIL
(MONOTONIC-INCREASING-P '())
T
Define a recursive function A-LIST-TEST, that
examines whether its argument, a list, is an associative list.
A list is associative if all its arguments on its top-level are genuine
sub-lists. In Principal we are dealing with a predicate that tests whether all
the elements in the list are sub-lists.
Examples::
(a-list-test '((a b) c (d e)))
NIL
(a-list-test '((a b) (c d) (e f)))
T
(a-list-test '(a t w z nil))
NIL
(a-list-test '())
T
Define a recursive function COUNT-ITEM , that has two arguments, an expression and an arbitrarily nested list. The function should count the number of occurances of that item in arbitrary positions in the list and its sub-lists.
Examples::
(count-item 'a '(s d (a) ((a) (b c))))
2
(count-item 'x '(s d (a) ((a) (b c))))
0
(count-item 'a '())
0
Define a recursive function LIST-UP-TO-ATOM which has a list as its argument. The function should return a list of the elements up to (and including) the first occurance of an atom in the top-level of the list. NIL should be treated as an atom in this context.
Examples::
(list-up-to-atom '((z x) s (d (f g))))
((Z X) S)
(list-up-to-atom '((a) (b) () (d)))
((A) (B) NIL)
(list-up-to-atom '((a) (b) (c) (d)))
((A) (B) (C) (D))
(list-up-to-atom '())
NIL