Friday, August 13, 2010

Lisp Like List in Java

Question

Implement Lisp-like list in Java. Write basic operations such as 'car', 'cdr', and 'cons'. If L is a list [3, 0, 2, 5], L.car() returns 3, while L.cdr() returns [0,2,5].


Lisp Operations

Car


The car of a list is, quite simply, the first item in the list. For Ex: car of L.car() of a list [3 0 2 5] returns 3.

Cdr


The cdr of a list is the rest of the list, that is, the cdr function returns the part of the list that follows the first item. For ex: cdr of a list [3 0 2 5] returns [0 2 5]

Cons


The cons function constructs lists. It is useful to add element to the front of the list; it is the inverse of car and cdr. For example, cons can be used to make a five element list from the four element list.

(source – Wikipedia)

Length


Returns the length of the list.


Program


import java.util.*;

import java.io.*;

// To implement LISP-like List in Java to perform functions like car, cdr, cons.

class Lisp

{

Vector v = new Vector(4,1);

int i;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader inData = new BufferedReader(isr);

String str;

//Create a LISP-Like List using VECTOR

public void create()

{

int n;

System.out.print("\n Enter the Size of the list:");

//Get Input from the user...Use I/p stream reader for it...

try

{

str = inData.readLine();

n = Integer.parseInt(str);

for(i=0;i

{

int temp;

System.out.print("\n Enter the element " + (i+1) + " of the list:");

str = inData.readLine();

temp=Integer.parseInt(str);

v.addElement(new Integer(temp));

}

}

catch (IOException e)

{

System.out.println("Error!");

System.exit(1);

}

}

//Display the content of LISP-List

public void display()

{

int n=v.size();

System.out.print("[ ");

for(i=0;i

{

System.out.print(v.elementAt(i) + " ");

}

System.out.println("]");

}

/*

The car of a list is, quite simply, the first item in the list.

For Ex: car of L.car() of a list [3 0 2 5] returns 3.

Gives the CAR of LISP-List

*/

public void car()

{

System.out.println("\n CAR of the List: " + v.elementAt(0));

}

/*

The cdr of a list is the rest of the list, that is, the cdr function returns the part of the list that follows the first item.

For ex: cdr of a list [3 0 2 5] returns [0 2 5]

*/

public void cdr()

{

int n=v.size();

System.out.print("\n The CDR of the List is:");

System.out.print("[ ");

for(i=1;i

{

System.out.print(v.elementAt(i) + " ");

}

System.out.println("]");

}

/*

The cons function constructs lists. It is useful to add element to the front of the list; it is the inverse of car and cdr. For example, cons can be used to make a five element list from the four element list.

For ex: adding 7 to [3 0 2 5], cons(7) gives [7 3 0 2 5]

*/

public void cons(int x)

{

v.insertElementAt(x,0);

System.out.print("\n The List after using CONS and adding an element:");

display();

}

// Returns the length of the LISP-List

public int length()

{

return v.size();

}

}

class Lisp_List

{

public static void main(String[] args)

{

Lisp a = new Lisp();

a.create();

System.out.print("\n The contents of the List are:");

a.display();

a.car();

a.cdr();

a.cons(5);

System.out.println("\n\n The length of the list is " + a.length());

}

}


Output


Enter the Size of the list:4

Enter the element 1 of the list:3

Enter the element 2 of the list:0

Enter the element 3 of the list:2

Enter the element 4 of the list:5

The contents of the List are:[ 3 0 2 5 ]

CAR of the List: 3

The CDR of the List is:[ 0 2 5 ]

The List after using CONS and adding an element:[ 5 3 0 2 5 ]

The length of the list is 5


Download the Source Code

No comments: