Please do it in Lisp, thanks a lot. Assume L is list of only integer numbers (In
ID: 3847460 • Letter: P
Question
Please do it in Lisp, thanks a lot. Assume L is list of only integer numbers (Including zeros, or positive, or negative numbers, or could be an empty list), write a function that finds and returns the rightmost even number from this (if any, otherwise return NIL). Example: for L= (-2, 7, 10, 9, -11, 31, 0, 57, 28, 1) , you should return: 28
My idea is to reverse the list first and then find the leftmost even number. However I couldn't combine them in one function and if I do it seperately the code won't be able to compile.
Explanation / Answer
; reverse function will return the reversed list
;dolist can be used to iterate through list and find the first even number
;The code is as follows
((defun myFunc (L))
(setq new_L(reverse L))
(dolist (num new_L)
(if (evenp num)
(return-from myFunc num))
))
(write (myFunc (L)))
; Assume L has the list as given in example above
;Output
;28