76498

recursive function lisp return list

Question:

I 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.

Answer1:

Notice that given the number of occurrences, for example 10, you can simply do

(make-list 10 :initial-element 'o)

or equivalently

(loop repeat 10 collect 'o)

To count the 'o in your list, you can do

(count 'o '(a b c o p o a z))

Thus, a simple solution for your function would be

(defun garde-o (a) (make-list (count 'o a) :initial-element 'o)) <hr />

However, you can do this recursively too

(defun garde-o (a) (cond ((null a) nil) ((eq (car a) 'o) (cons 'o (garde-o (cdr a)))) (t (garde-o (cdr a)))))

and non-recursively

(defun garde-o (a) (loop for x in a when (eq x 'o) collect x))

Recommend

  • sending parameters with ajax
  • How to ngFor in angular2
  • Create, Update and Delete in Django
  • LineChart FX - Delete solid line
  • Inject functions with side-effects
  • Returned list is redundant after recursion
  • SCHEME Mutable Functions
  • PHP: str_replace() - ignore white space
  • codeigniter active records join with using?
  • Find position first occurence of an array of strings in a string
  • Language Scheme: find the sum of proper divisors
  • returns the first n of list
  • Remove every nth element from swift array
  • Easier way to do non-recursive list on gcs bucket outside appengine
  • 1º Day of Daylight Saving Time Java and JS showing a different behavior
  • Is there a way to ensure one object reference per record in an ActiveRecord hierarchy?
  • How do `pass` and `listen` work in WriterT?
  • JDBI's @BindBean doesn't find named parameters in bean class during INSERT
  • Using ActiveRecord and Rails to insert Data into postgresql database get this error: RuntimeError: E
  • reduce/reduce conflicts using ocamlyacc
  • Web service not returning integer data in an object [duplicate]
  • How to write string.Contains(someText) in expression Tree
  • How to get the index of element in the List in c#
  • Parse Framework with Swift
  • Rails AREL .where statement
  • Creating JS objects in PHP with commas in between
  • Binary trees in scheme
  • Regex for Specific Tag
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Xstream to map “choice” elements of XML
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • C# - Getting references of reference
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?