Results 1 to 1 of 1
Thread: How to implement a LispList?
- 09-14-2012, 05:02 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
How to implement a LispList?
Hi everyone,
I've been working on Java problems over the summer, and I haven't found anything especially hard, but here's a problem I'm not sure how to start implementing.
The LISP language implements linked lists in a very elegant way. There is a Java version of this. The tail of an abstract list(list with head removed) is also a list. It goes on and on, until an empty list is reached. An interface of this would be:
There are two kinds of lists:Java Code:public interface LispList{ boolean isEmpty(); Object head(); LispList tail(); . . . }
EmptyList has no instance variables. Head and tail methods simply throw unsupported operation exception and isEmpty returns true. NonEmptyList has instance variables for head and tail.Java Code:public class EmptyList extends LispList{ ... } public class NonEmptyList extends LispList{ ... }
One way of implementing one would be:
It would be a good idea to make a function cons(Object obj) that builds up from the tail:Java Code:LispList list = newNonEmptyList("A", new NonEmptyList("B", newNonEmptylist("C", new EmptyList())));
So far, I have this:Java Code:LispList list = LispList.NIL.cons("C").cons("B").cons("A");
Java Code:public interface LispList { boolean isEmpty(); Object head(); LispList tail(); String toString(); class Node { public Object data; public Node next; } }Java Code:public class EmptyList implements LispList{ public EmptyList() { } public boolean isEmpty() { return true; } public Object head() { return null; } public LispList tail() { return null; } public String toString() { return ""; } }Question is:Java Code:public class NonEmptyList implements LispList { private Node head; private Node tail; public NonEmptyList(Object o) { head = new Node(); head.data = o; head.next = tail; } public boolean isEmpty() { return false; } public Object head() { return head.data; } public void cons(Object o) { Node newNode = new Node(); newNode.data = o; newNode.next = null; tail = newNode; } public LispList tail() { removeHead(); return this; } private void removeHead() { head = head.next; } public String toString() { return head() + " " + tail().toString(); } }
How do I get cons to work? Also, I don't understand how .NIL should work too.
Also, I have to add in a function that returns the length, a a function that merges two lisplists together ( 1 2 3 4 + 5 6 = 1 5 2 6 3 4 ), and if it contains a certain element.
I would be extremely grateful for some help.
Similar Threads
-
Implement an Iterator
By sehudson in forum New To JavaReplies: 5Last Post: 03-14-2011, 12:02 AM -
Need Help to Implement JWebmail
By pintushahk in forum Advanced JavaReplies: 0Last Post: 12-08-2010, 10:14 PM -
Help to implement JTable
By adeeb in forum AWT / SwingReplies: 0Last Post: 06-04-2008, 06:26 PM -
How to implement HttpSessionListener in JSP
By Srikanth816 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-16-2008, 09:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks