Question

In Lisp programming language

a 16. (20 pts.) Write a function that takes a list of 0s and ls and toggles each element. Your function should include la

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

Function:

(defun toggle(l)
(mapcar (lambda(x) (mod (+ x 1) 2)) l)
)

Toggle logic:

for each x do : (x+1)%2

mapcar operates on successive elements of the lists. function is applied to the first element of each list, then to the second element of each list, and so on.

Sample execution:

(defun toggle(1) (mapcar (lambda(x) (mod (+ x 1) 2)) 1) ) 1 2 3 4 5 6 (write(toggle (list 0 0 1 1 0 1)))

$clisp main.lisp (1 10 010)

Add a comment
Know the answer?
Add Answer to:
In Lisp programming language a 16. (20 pts.) Write a function that takes a list of...
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
ADVERTISEMENT