Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Ex3 lisp likelist in java
1. 3.Implementation of Lisp_like list in java
AIM:
To write a java program to implement the lisp_like list in java.
ALGORITHM:
1.To implement the necessary packages needed.
2.Implement the lisp_like list in java to perform functions like that
car(),cdr()
3.Car() function
The car of a list is, quite simply, the first item in the list.
Thus the car of the list (rose violet daisy
buttercup) is rose.
Example:
(car '(rose violet daisy buttercup))
•After evaluating the expression, rose will appear in the
echo area.
4.Cdr() function
•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.
Example:
(cdr '(rose violet daisy buttercup))
•When you evaluate this, (violet daisy buttercup)
will appear in the echo area.
5.To create a arraylist and add the integer values
6.To perform the car()&cdr() functions in above method ;
7.Print the outputs and terminate the program.
PROGRAM:
import java.util.*;
/**
2. * @version 1.6.0
* @author Annal Sheeba Rani
*/
//To create a class Lisp using to perfom car()&cdr()functions
public class Lisp {
//To perform car() function
public int car(List l){
Object ob=l.get(0);
String st=ob.toString();
System.out.println("Using a L.car() function");
return Integer.parseInt(st);
}
//To perform cdr() function
public List cdr(List l){
Object ob=l.remove(0);
Object obj[]=l.toArray();
System.out.println("Using a L.cdr() function");
List list=Arrays.asList(obj);
return list;
}
public static void main(String[] args)
{
// To add the integer value in the ArrayList
List<Integer> l=new ArrayList<Integer>();
l.add(3);
3. l.add(0);
l.add(2);
l.add(5);
//To print the Return values of Car(),Cdr()functions
Lisp L=new Lisp();
int val=L.car(l);
System.out.println(val);
List list=L.cdr(l);
System.out.print(list);
}
}
OUTPUT:
Using a L.car() function
3
Using a L.cdr() function
[0, 2, 5]