1) The snippet of code (in C) to implement "clone" function:
// clone simple list
int clone(int ls){
if( ls == NIL ) return NIL;
else return cons( head(ls), clone(tail(ls)));
}
// clone complex list
int clone2(int ls){
if( ls == NIL ) return NIL;
if (isatom(head(ls)) return cons(head(ls), clone2(tail(ls)));
else return cons( clone2(head(ls)), clone2(tail(ls)));
}
Exercise your brain on the following not too difficult problems:
1) write a recursive program to sum a simple list of integer. For example sum(1 2 3 4) returns 10.
1s) write similar program to 1) but for a complex list. For example sum((1 2) (3 4)) returns 10.
Now, a more difficult one. These problems require "creating" a list.
2) write a recursive program to "translate" a simple list of integer. Change all occurrence of "1" to "10". For example trans(1 2 3 4 1 5 6) returns (10 2 3 4 10 5 6).
2s) write similar program to 2) but for a complex list. For example trans( (1 2 3) (4 1 5 6)) returns ( (10 2 3) (4 10 5 6)).
last update 3 Sept 2015