1

know this is a newbie question I apologize in advance. I'm writing a recursive function which returns the number of 'o in a given list

(defun garde-o (liste)
    (cond
        ((not liste) 0) 
        ((equal (car liste) 'o)  (+ 1 (garde-o(cdr liste)))   )
        ((garde-o(cdr liste))  )
    )
)

Instead of returning the number of occurence I would like to return the given list with only the 'o.

Like that:

(garde-o '(a o x & w o o)) should return => (o o o)

I don't want to use pop,push,set... just I can't find of to return this.

1
  • You should avoid re-asking question if down voted earlier. Correct your question and someone will help you. You have already posted :stackoverflow.com/questions/28494515/… Commented Feb 13, 2015 at 9:45

2 Answers 2

1

Your current version returns a number which is incremented on each recursion. So your proposed version should work by building a list, extending it on each recursion.

Replace 0 with ‘(), and + 1 with cons ‘o.

Sign up to request clarification or add additional context in comments.

Comments

0

You are very close,

(defun garde-o (liste)
 (cond ((not liste) nil)
  ((equal (car liste) 'o) (cons (car liste) (garde-o (cdr liste))))
  ((garde-o (cdr liste)))))

Usage: (garde-o '(a o x & w o )) => ( o o )

You just need to pick on your identified car.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.