Program
import java.util.*;
// To implement LISP-like List in Java to perform functions like car, cdr, cons.
class Lisp
{
public Vector car(Vector v)
{
Vector t = new Vector();
t.addElement(v.elementAt(0));
return t;
}
public Vector cdr(Vector v)
{
Vector t = new Vector();
for(int i=1;i
return t;
}
public Vector cons(String x, Vector v)
{
v.insertElementAt(x,0);
return v;
}
public Vector cons(int x, Vector v)
{
v.insertElementAt(x,0);
return v;
}
}
class Lisp_List2
{
public static void main(String[] args)
{
Lisp a = new Lisp();
Vector v = new Vector(4,1);
Vector v1 = new Vector();
System.out.println("\n Creating a List of Strings....\n");
v.addElement("S.R.Tendulkar");
v.addElement("M.S.Dhoni");
v.addElement("V.Sehwag");
v.addElement("S.Raina");
System.out.println("\n The Contents of the List are " + v);
System.out.print("\n The CAR of this List....");
System.out.println(a.car(v));
System.out.print("\n The CDR of this List....");
v1 = a.cdr(v);
System.out.println(v1);
System.out.println("\n The Contents of this list after CONS..");
v1 = a.cons("Gambhir",v);
System.out.print(" " + v1);
v.removeAllElements();
v1.removeAllElements();
System.out.println("\n\n Creating a List of Integers....\n");
v.addElement(3);
v.addElement(0);
v.addElement(2);
v.addElement(5);
System.out.println("\n The Contents of the List are " + v);
System.out.print("\n The CAR of this List....");
System.out.println(a.car(v));
System.out.print("\n The CDR of this List....");
v1 = a.cdr(v);
System.out.println(v1);
System.out.println("\n The Contents of this list after CONS..");
v1 = a.cons(9,v);
System.out.print(" " + v1);
}
}
Output
Creating a List of Strings....
The Contents of the List are [S.R.Tendulkar, M.S.Dhoni, V.Sehwag, S.Raina]
The CAR of this List....[S.R.Tendulkar]
The CDR of this List....[M.S.Dhoni, V.Sehwag, S.Raina]
The Contents of this list after CONS..
[Gambhir, S.R.Tendulkar, M.S.Dhoni, V.Sehwag, S.Raina]
Creating a List of Integers....
The Contents of the List are [3, 0, 2, 5]
The CAR of this List....[3]
The CDR of this List....[0, 2, 5]
The Contents of this list after CONS..
[9, 3, 0, 2, 5]
Download Source Code
2 comments:
at last..got ur prog done??hmm goodd..
Ya...He want some modifications in this too.....
Post a Comment