Question

The programming language is Emacs-Lisp. Implement a function in Lisp that takes in one parameter that...

The programming language is Emacs-Lisp.

Implement a function in Lisp that takes in one parameter that is a list and returns the previous to last element of the list.

If the list has less than two elements, it should return nil. Use the while loop for this function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

check out the solution and do comment if any queries.

----------------------------------------

; function definition
(defun lastele (L)
; while loop - loop through list (havig length > 2) till it reaches the list length == 2
(loop while (> (list-length L) 2)
do (setq L (cdr L)))
; check for list length == 2
(if (eq (list-length L) 2)
; if true then return the first element of the list which in turn is 2nd last element
(car L)))

; function call and display accordingly
(print (lastele '(1 2 3 4 5 6 7)))
(print (lastele ()))   
(print (lastele '(1 2)))
(print (lastele '(1)))

--------------------------------------------------

Code :

-----------------------

Output :

Add a comment
Know the answer?
Add Answer to:
The programming language is Emacs-Lisp. Implement a function in Lisp that takes in one parameter that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT