Please place all of the following functions (defun …) into a SINGLE .lsp file As
ID: 3827197 • Letter: P
Question
Please place all of the following functions (defun …) into a SINGLE .lsp file
As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide
Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number.
Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB to help you here.
Write a function GETELEM that returns that takes two parameters, the first being a list and the second being an integer n, and returns the nth element of that list, whatever it is (list or atom).
Write a function DELELEM that takes 2 parameters, the first being an integer n and the second being a list, and returns a list equivalent to the list passed in with the nth element removed.
Write a function MAXIMUM that takes a simple list of integers as a parameter, and returns the largest (Do not worry about efficiency here).
Write a function COUNTELEMS that returns the number of elements in a list.
Write a function ISPAL that accepts a list as an argument and returns t if the list is a palindrome and nil if it is not. You can assume that each element of the list is a single character atom. HINT: There is a built in function REVERSE that reverses a list.
Write a function FLAT that takes a list as a parameter and breaks all of its’ elements down into atoms, and returns the result (in order). This should also deal with sublists in sublists etc. For example (X Y (Z A) B ((C D E) F)) passed in as the parameter should return (X Y Z A B C D E F).
Please submit a single .lsp file with all of these functions in it. You should test your work on empress.csusm.edu in clisp by loading your file and calling your functions with a variety of different parameters.
Explanation / Answer
1. Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number.
int ONEFIB(int n){
if (n==0){
return 0;
}
else if(n==1){
return 1;
}
else{
return(ONEFIB(n-1) + ONEFIB(n-2)); // it recursively finds nth fibonacci number
}
}
2. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers.
int ALLFIB(int n){
for(int count = 0; count < n; count++){ // recursively finds count'th fibonacci number and prints every number.
return ONEFIB(count)); // we are using ONEFIB(int n) function
}
}
3. Write a function GETELEM that returns that takes two parameters, the first being a list and the second being an integer n, and returns the nth element of that list
int GETELEM(List<Book> books, int n){ //passing list and n as arguments
List<Book> books = new ArrayList<Book>(); //array list declaration
int size = books.size(); // to print size of the list
books.get(n-1); //since array starts with index 0
}
4. Write a function DELELEM that takes 2 parameters, the first being an integer n and the second being a list, and returns a list equivalent to the list passed in with the nth element removed.
int DELELEM(List<Book> books, int n){ //passing list and n as arguments
List<Book> books = new ArrayList<Book>(); //array list declaration
int size = books.size(); // to print size of the list
books.remove(n-1); //since array starts with index 0
return books;
}
5. Write a function MAXIMUM that takes a simple list of integers as a parameter, and returns the largest.
int MAXIMUM(List<Book> books){ //passing list as an argument
List<Book> books = new ArrayList<Book>(); //array list declaration
int max = books.get(0);
for(int i = 0; i < books.size(); i++) { // comparing every element to get max
n = books.get(i);
if(n < max){
max = n;
}
}
}
6. Write a function COUNTELEMS that returns the number of elements in a list.
int COUNTELEMS(List<Book> books){
List<Book> books = new ArrayList<Book>(); //array list declaration
int size = books.size(); // size of list is nothing but the count of elements
return size;
}
}