Question: I d like to ask an expert The LISP language, created in 1 9 5 8 by John McCarthy ( MIT ) , implements linked
Id like to ask an expert
The LISP language, created in by John McCarthy MITimplements linked lists in a very elegant way. You will explore a Java analog in this additional part. Conceptually, the tail of a list that isthe list with its head node removed is also a list. The tail of that list is again a list and so onuntil you reach the empty list.
Here is a tentative Java interface for such a list:
A list of entries of some number of homogenouslytyped elements @code Apublic interface LispList a "static factory" method for producing an empty list instance static boolean emptyreturn new EmptyList; boolean isEmpty; String head; NOTE: unlike your BST above, our LispList can only hold Strings LispList tail;
There are two kindsvarieties of a LispList, empty and nonempty lists:
public class EmptyList implements LispList public class NonEmptyList implements LispList
These classes are pretty trivial. The EmptyList has no instance variablesfieldsIts head and tail methods simply throw an UnsupportedOperationException and its empty method returns true. The NonEmptyList class will need to store fields to track the head and tail.
Here is one way of making a LISP list with three elements:
LispList list new NonEmptyListAnew NonEmptyListBnew NonEmptyListCnew EmptyList;
This is a bit tedious, and it is a good idea to supply a convenience method, cons that calls the constructor, as well a static variable Nil that is an instance of the empty list.
Then the list construction code becomes:
toString will ideally give: ABCLispList list LispListemptyconsCconsBconsA;
breaking down what's going on above:
We'll look at the construction of the above list:
Sostarting with the empty list we get:
new EmptyList
A new list is constructed with C as the head and Nil as the tail:
CEmptyList high level view" new LispListCnew EmptyListstructure view"
Next, a new list is constructed with B as the head and the previous list:
BCNil new LispListBnew LispListCnew EmptyList
Next the A gets tacked on the front, etc.
Some exercises
These should all be done in a separate file called LispListExercises and the methods should all be marked static. Use recursion for all of these write the unit tests as you work through these.
method: length
Takes a LispList as a parameter and computes and returns the length of the list.
lengthABC
method: contains
Takes a LispList and some String key and and returns true only if key appears in the list; false otherwise.
containsABCBtrue
method: delete
Takes a LispList as well as the item to be removed and returns a LispList with the item removed.
deleteABCBreturned list: ACdeleteABCDreturned list: ABC
method: cat
Takes two LispLists and appends them together and returns the resulting concatenatedlist
catABCDABCDcatXYXY
method: merge
Takes two LispLists and alternates between the elements, adding the remainder to the end.
mergeACEBDFABCDEFmergeABAB
note: the numbers in the second example will just be strings, ie:
in java
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
