Author Topic: A recursion way of coding  (Read 4664 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: A recursion way of coding
« Reply #15 on: October 20, 2013, 07:44:49 AM »
Another tail-recursive version:
Code - Auto/Visual Lisp: [Select]
  1. (defun odd ( l / foo )
  2.     (defun foo ( l a )
  3.         (cond
  4.             (   (null l)
  5.                 (reverse a)
  6.             )
  7.             (   (listp (car l))
  8.                 (foo (cdr l) (append (reverse (odd (car l))) a))
  9.             )
  10.             (   (= 1 (rem (car l) 2))
  11.                 (foo (cdr l) (cons (car l) a))
  12.             )
  13.             (   (foo (cdr l) a))
  14.         )
  15.     )
  16.     (foo l nil)
  17. )
Example:
Code - Auto/Visual Lisp: [Select]
  1. _$ (odd '(1 2 (3 4 5 6 7) 8 9))
  2. (1 3 5 7 9)

Coder

  • Swamp Rat
  • Posts: 827
Re: A recursion way of coding
« Reply #16 on: October 20, 2013, 09:28:20 AM »
Very great works experts  :-)

All codes work very nice , Fantastic .

I was in a hurry today and forgot to mention if it is possible to include the brackets from the sub-list .  :ugly: :oops:

Example :
Quote
(1 (3 5 7) 9)

Many many thanks , and please bear with me guys .

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: A recursion way of coding
« Reply #17 on: October 20, 2013, 09:48:24 AM »
I was in a hurry today and forgot to mention if it is possible to include the brackets from the sub-list .  :ugly: :oops:

Example :
Quote
(1 (3 5 7) 9)

My code updated:
Code - Auto/Visual Lisp: [Select]
  1. (defun odd ( l )
  2.     (vl-remove nil
  3.         (mapcar
  4.             (function
  5.                 (lambda ( x )
  6.                     (if (numberp x)
  7.                         (if (= 1 (rem x 2)) x)
  8.                         (odd x)
  9.                     )
  10.                 )
  11.             )
  12.             l
  13.         )
  14.     )
  15. )
Code - Auto/Visual Lisp: [Select]
  1. (defun odd ( l )
  2.     (if (numberp l)
  3.         (if (= 1 (rem l 2)) l)
  4.         (if l (vl-remove nil (cons (odd (car l)) (odd (cdr l)))))
  5.     )
  6. )
Code - Auto/Visual Lisp: [Select]
  1. (defun odd ( l / foo )
  2.     (defun foo ( l a )
  3.         (cond
  4.             (   (null l)
  5.                 (reverse a)
  6.             )
  7.             (   (listp (car l))
  8.                 (foo (cdr l) (cons (odd (car l)) a))
  9.             )
  10.             (   (= 1 (rem (car l) 2))
  11.                 (foo (cdr l) (cons (car l) a))
  12.             )
  13.             (   (foo (cdr l) a))
  14.         )
  15.     )
  16.     (foo l nil)
  17. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (odd '(1 2 (3 4 5 6 7) 8 9))
  2. (1 (3 5 7) 9)

Coder

  • Swamp Rat
  • Posts: 827
Re: A recursion way of coding
« Reply #18 on: October 20, 2013, 11:05:12 AM »
A very great work Lee ,
thank you so much for your kind reply and rare generosity . :-)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: A recursion way of coding
« Reply #19 on: October 20, 2013, 11:33:33 AM »
You're welcome Coder  :-)