Use recursion in your solution The following lisp functions
Use recursion in your solution. The following lisp functions are NOT ALLOWED: for, until, do, do*, dolist, setq, reverse, and when
1. write a lisp function palindrome that takes a list \'1st\' as input and returns t if the 1st has a sequence of elements which reads the same backwards and forward.
2. Use filter to write a Lisp function namely palindrome-filter that takes an input list and returns the list of elements from the original list that are palindromes evaluated by the Lisp function palindrome given in the answer of Q1.
3. Write a lisp function palindrome-filter-b that takes the Lisp function palindrome given the answer of Q1 and a list, and returns the list of elements of t or nil from the original list for which the function palindrome evaluates the elements t or nil. Hint: use mapcar to write the function.
4. Write a Lisp function prime that takes a positive integer(>=1) as input and returns t if the integer is a prime otherwise nil.
5. Write a Lisp function generate-numbers that generates a list of integers in the range of first and last.
6. Write a Lisp function namely prime-filter that takes an input list and returns the list of elements from the original list that are palindromes evaluatd by the Lisp function palindrome given in the answer of Q1. Make sure your answer in Q1 is correct. Hint: use filter to implement the function
Solution
1. write a lisp function palindrome that takes a list \'1st\' as input and returns t if the 1st has a sequence of elements which reads the same backwards and forward.
# Program -Using string
# is palindrome or not
# change this value for a different output
my_str = \'aIbohPhoBiA\'
# make it suitable for caseless comparison
my_str = my_str.casefold()
# reverse the string
rev_str = reversed(my_str)
# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
print(\"It is palindrome\")
else:
print(\"It is not palindrome\")
2. Use filter to write a Lisp function namely palindrome-filter that takes an input list and returns the list of elements from the original list that are palindromes evaluated by the lisp function
an iterative approach
(defun make-palindrome(list)
(if (= (length list) 1)
list
(dotimes (count (length list) list)
(setf list (append list (list (nth (- (length list) (+ count (+ count 1))) list))))
))
)
3. Write a lisp function palindrome-filter-b that takes the Lisp function palindrome given the answer of Q1 and a list, and returns the list of elements of t or nil from the original list for which the function palindrome evaluates the elements t or nil. Hint: use mapcar to write the function.
(defun palindrome-p (list)
(let* ((length (length list))
;; The following lists will NOT include the middle element if length is odd
(half-length (ceiling length 2))
(tail (nthcdr half-length list))
(reversed-head (nreverse (butlast list half-length))))
(equal tail reversed-head)))
4. Write a Lisp function prime that takes a positive integer(>=1) as input and returns t if the integer is a prime otherwise nil
#!/usr/bin/python
num = input(\'Enter the number which you wish to determine it is prime: \')
for div in range(2, (num/2)+1):
if num%div==0:
print \"\ The Number\", num, \" is not a prime!\"
break
else:
print \"\ The Number\", num, \" is a prime!\"
5. Write a Lisp function generate-numbers that generates a list of integers in the range of first and last.
(defun range (max &key (min 0) (step 1))
(loop for n from min below max by step
collect n))
This allows you to specify an (optional) minimum value and an (optional) step value.
To generate odd numbers: (range 10 :min 1 :step 2)
6. Write a Lisp function namely prime-filter that takes an input list and returns the list of elements from the original list that are palindromes evaluatd by the Lisp function palindrome given in the answer of Q1
Python Code:
Editor:
def sum(numbers):
total = 0
for x in numbers:
total += x
return total
print(sum((8, 2, 3, 0, 7)))

