Question

1. Write a recursive procedure called instanceCount that counts the number of instances of a number...

1. Write a recursive procedure called instanceCount that counts the number of instances of a number in a list

(instanceCount '(1 2 3 4 3 2 1) 3) => 2

2. Write a recursive procedure called excludeElement that receives a list and a number. It returns a new list with all instances of that number removed

(excludeElement '(1 2 3 4 3 2 1) 3) => (1 2 4 2 1)

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

1)

Write a recursive procedure called instanceCount that counts the number of instances of a number in a list

(instanceCount '(1 2 3 4 3 2 1) 3) => 2

lisp code:

(defun instanceCount (l n)
; declaring variable c with value 0
(setq c 0)
; for loop for item in list
(loop for i in l
; condition for checking equal
do(if(= i n)
; condition true the c value incremenet
(incf c 1)))
(write c)
)

(instanceCount '(1 2 3 4 3 2 1) '3)

output:

2

2). Write a recursive procedure called excludeElement that receives a list and a number. It returns a new list with all instances of that number removed

(excludeElement '(1 2 3 4 3 2 1) 3) => (1 2 4 2 1)

lisp code:

(defun excludeElement (l n)
; creating list
(setq mynewlist '())
; for llop for given list
(loop for x in l
; condition check if item not equal to given num then append to new list
do(setq mynewlist (append mynewlist (if (/= x n) (list x))))
)
; printng new list
(write mynewlist)
)

(excludeElement '(1 2 3 4 3 2 1) '3)

ouput:

(1 2 4 2 1)

Note:

  • ; indicates comment for code line
Add a comment
Know the answer?
Add Answer to:
1. Write a recursive procedure called instanceCount that counts the number of instances of a number...
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