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

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
A recursion way of coding
« on: October 07, 2013, 03:30:32 PM »
Hello experts .

I would like to know how to deal with recursion way of coding , for example ,

I would like to get only the singular position of this list .
Code: [Select]
'(1 2 3 4 5 6 7 8 9)
should return :
Code: [Select]
(1 3 5 7 9)

Many thanks

reltro

  • Guest
Re: A recursion way of coding
« Reply #1 on: October 07, 2013, 03:40:03 PM »
hey,

what should happen?
u just want the odd ones?

Code: [Select]
(vl-remove-if
   '(lambda (a / )
      (= (rem a 2) 0)
   )
   '(1 2 3 4 5 6 7 8 9)
)
:|

no recursion needed...

Coder

  • Swamp Rat
  • Posts: 827
Re: A recursion way of coding
« Reply #2 on: October 07, 2013, 03:46:17 PM »
no recursion needed...

I just want to understand the way of recursion coding , that's it .  :wink:

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: A recursion way of coding
« Reply #3 on: October 07, 2013, 03:48:01 PM »
Not bad response time 10 mins....

Bruce

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: A recursion way of coding
« Reply #4 on: October 07, 2013, 03:53:43 PM »
Hi,

Not sure to understand the goal.

To get odd numbers:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo (l)
  2.   (cond
  3.     ((null l) nil)
  4.     ((zerop (rem (car l) 2)) (foo (cdr l)))
  5.     (T (cons (car l) (foo (cdr l))))
  6.   )
  7. )

To get one of two item:
Code - Auto/Visual Lisp: [Select]
  1. (defun bar (l)
  2.   (if l
  3.     (cons (car l) (bar (cddr l)))
  4.   )
  5. )
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: A recursion way of coding
« Reply #5 on: October 07, 2013, 04:00:54 PM »
Very nice codes gile  :-)

I hope more users would post more codes .

Many thanks

reltro

  • Guest
Re: A recursion way of coding
« Reply #6 on: October 07, 2013, 04:06:33 PM »
nice code gile :)

same as gile, but the recursion solved in an other manner...

Code: [Select]
(   (   (lambda (rec / )
         (eval
            (list 'lambda '(lst / )
               (list rec rec 'lst)
            )
         )
      )
      (lambda (rec lst / )
         (if (not (null lst))
            (cons
               (car lst)
               (rec rec (cddr lst))
            )
         )
      )
   )
   '(1 2 3 4 5 6 7 8 9)
)

(   (   (lambda (rec / )
         (list 'lambda '(lst / )
            (list rec rec 'lst)
         )
      )
      (lambda (rec rec lst / )
         (cond
            ((null lst) nil)
            ((zerop (rem (car lst) 2)) (rec rec (cdr lst)))
            ('default (cons (car lst) (rec rec (cdr lst))))
         )
      )
   )
   '(1 2 3 4 5 6 7 8 9)
)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: A recursion way of coding
« Reply #7 on: October 07, 2013, 04:08:24 PM »
Another simple recursive function to understand the methodology
Code: [Select]
(defun recursion (l v)
  (if l
    ;;; pass the truncated list of the second and remaining items and a list of response items
    (recursion (cddr l) (append v (list(car l))))
    ;;; output the list of extracted items if l is nil
    v
  )
)
« Last Edit: October 08, 2013, 10:08:02 AM by Keith™ »
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: A recursion way of coding
« Reply #8 on: October 08, 2013, 07:45:01 AM »
I would likely code it in the same way as gile, but to offer a few tail-recursive variations:

Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l / bar )
  2.     (defun bar ( l a )
  3.         (if l
  4.             (if (zerop (rem (car l) 2))
  5.                 (bar (cdr l) a)
  6.                 (bar (cdr l) (cons (car l) a))
  7.             )
  8.             (reverse a)
  9.         )
  10.     )
  11.     (bar l nil)
  12. )
[Condensed]:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l / bar )
  2.     (defun bar ( l a )
  3.         (if l
  4.             (bar (cdr l) (if (zerop (rem (car l) 2)) a (cons (car l) a)))
  5.             (reverse a)
  6.         )
  7.     )
  8.     (bar l nil)
  9. )
Every other:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l / bar )
  2.     (defun bar ( l a )
  3.         (if l
  4.             (bar (cddr l) (cons (car l) a))
  5.             (reverse a)
  6.         )
  7.     )
  8.     (bar l nil)
  9. )
« Last Edit: October 08, 2013, 07:48:43 AM by Lee Mac »

Coder

  • Swamp Rat
  • Posts: 827
Re: A recursion way of coding
« Reply #9 on: October 08, 2013, 08:22:16 AM »
Waw great job from you all .

Keith , one missing paren for your routine , although I know it is tutorial of course , many thanks .

Thank you all experts .  :-)

Coder

  • Swamp Rat
  • Posts: 827
Re: A recursion way of coding
« Reply #10 on: October 19, 2013, 05:04:18 AM »
Hello .

If I have a list in a list it does not return the correct list .
Code: [Select]
(bar '(1 2 (3 4 5 6 7) 8 9))
it returns
Code: [Select]
(1 (3 4 5 6 7) 9)
But I expected to see this .
Code: [Select]
(1 3 5 7 9)
Can anyone give any example to iterate though list in a list please ?

Many thanks experts  :-)

ribarm

  • Gator
  • Posts: 3282
  • Marko Ribar, architect
Re: A recursion way of coding
« Reply #11 on: October 19, 2013, 06:04:25 AM »
Hello .

If I have a list in a list it does not return the correct list .
Code: [Select]
(bar '(1 2 (3 4 5 6 7) 8 9))
it returns
Code: [Select]
(1 (3 4 5 6 7) 9)
But I expected to see this .
Code: [Select]
(1 3 5 7 9)
Can anyone give any example to iterate though list in a list please ?

Many thanks experts  :-)

First, flatten list and then apply above posted function...

LM:Flatten sub-function :
(look whole thread - there are some interesting approaches)
http://forums.augi.com/showthread.php?129800-Lists-within-lists-simplify/page2&highlight=lists+list&p=#16

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: A recursion way of coding
« Reply #12 on: October 20, 2013, 06:25:54 AM »
Can anyone give any example to iterate though list in a list please ?

Here is a quick example using mapcar:
Code - Auto/Visual Lisp: [Select]
  1. (defun odd ( l )
  2.     (apply 'append
  3.         (mapcar
  4.             (function
  5.                 (lambda ( x )
  6.                     (if (numberp x)
  7.                         (if (= 1 (rem x 2)) (list x))
  8.                         (odd x)
  9.                     )
  10.                 )
  11.             )
  12.             l
  13.         )
  14.     )
  15. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (odd '(1 2 (3 4 5 6 7) 8 9))
  2. (1 3 5 7 9)
However, using mapcar, the function will be incompatible with dotted-pairs.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: A recursion way of coding
« Reply #13 on: October 20, 2013, 06:44:51 AM »
Another, for dotted pairs:
Code - Auto/Visual Lisp: [Select]
  1. (defun odd ( l )
  2.     (if (numberp l)
  3.         (if (= 1 (rem l 2)) (list l))
  4.         (if l (append (odd (car l)) (odd (cdr l))))
  5.     )
  6. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (odd '(1 2 (3 4 5 6 7) (8 . 9)))
  2. (1 3 5 7 9)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: A recursion way of coding
« Reply #14 on: October 20, 2013, 06:45:22 AM »
Code: [Select]
(defun odd (l)
  (cond
    ((null l) nil)
    ((vl-consp (car l)) (append (odd (car l)) (odd (cdr l))) )
    ((zerop (rem (car l) 2)) (odd (cdr l)))
    (T (cons (car l) (odd (cdr l))))
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12915
  • 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: 12915
  • 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: 12915
  • London, England
Re: A recursion way of coding
« Reply #19 on: October 20, 2013, 11:33:33 AM »
You're welcome Coder  :-)