import java.util.Scanner; class UseLispLists6 { public static void main(String[] args) { Scanner in = new Scanner(System.in); LispList ls; System.out.print("Enter a list (of integers): "); String str = in.nextLine(); ls = parseIntLispList(str); System.out.print("The list you entered is: "); System.out.println(ls); System.out.print("Enter two integers: "); int m = in.nextInt(); int n = in.nextInt(); LispList ls1 = change(ls,m,n); System.out.println("Changing all occurrences of "+m+" to "+n+ " in the list gives:"); System.out.println(ls1); } public static LispList change(LispList ls1,T m,T n) // Changes all occurrences of m to n in ls1 { LispList ls2 = LispList.empty(); for(; !ls1.isEmpty(); ls1=ls1.tail()) { T h = ls1.head(); if(h.equals(m)) ls2 = ls2.cons(n); else ls2 = ls2.cons(h); } LispList ls3 = LispList.empty(); for(; !ls2.isEmpty(); ls2=ls2.tail()) ls3 = ls3.cons(ls2.head()); return ls3; } public static LispList parseIntLispList(String str) { String line = str.trim(); String contents = line.substring(1,line.length()-1).trim(); if(contents.length()==0) return LispList.empty(); String[] nums = contents.split(","); LispList list = LispList.empty(); for(int i=nums.length-1; i>=0; i--) { String num = nums[i].trim(); list = list.cons(Integer.parseInt(num)); } return list; } }