Sunday, February 12, 2012

IMPLEMENTING A LISP LIKE LIST [ java ]


IMPLEMENTING  A LISP LIKE LIST

AIM:
           
To write a java program to implementing a lisp like list

ALGORITHM:
           
1.Create the class list and declare the variables.
            2.Generate an array using generic list method.
            3.Use add() function to add the elements in the generic array list.
            4.Get the values from list and store it in object.
            5.Use toString method to convert object as string and return the list.

PROGRAM:
import java.util.*;
class Lisp
{
            public int car(List l)
            {
                        Object ob=l.get(0);
                        String st=ob.toString();
                        return Integer.parseInt(st);
            }
            public List cdr(List l)
            {
                        Object ob=l.remove(0);
                        Object obj[]=l.toArray();
                        List list=Arrays.asList(obj);
                        return list;
            }
            public static void main(String[] args)
            {
                        List<Integer> l=new ArrayList<Integer>();
                        l.add(3);
                        l.add(0);
                        l.add(2);
                        l.add(5);
                        Lisp L=new Lisp();
                        int val=L.car(l);
                        System.out.println(val);
                        List list=L.cdr(l);
                        System.out.println(list);
            }
                       
}



OUTPUT:

S:\>javac lisp.java
S:\>java lisp
3
[0, 2, 5]

































RESULT:
Thus the java program to implement a lisp like list was done.

No comments:

Post a Comment