Author Topic: Can make this codes shorter ? using apply, mapcar or recursive ?  (Read 359 times)

0 Members and 1 Guest are viewing this topic.

drawdraw

  • Mosquito
  • Posts: 4
Can make this codes shorter ? using apply, mapcar or recursive ?
« on: November 17, 2020, 08:52:12 PM »
Code - Auto/Visual Lisp: [Select]
  1. (setq lst2 '())
  2. (setq lst '(10000.0 2000.0 3000.0 3000.0 2000.0))
  3. (defun haha (m)
  4.   (repeat (- (length m) 1)
  5.     (setq n (+ (car m) (cadr m)))
  6.     (setq lst2 (cons n lst2))
  7.     (setq m (cddr m))
  8.     (setq m (cons n m))
  9.     )
  10.   (setq lst2 (reverse lst2))
  11.   )
  12.  
  13. (haha lst)
  14. ; Return value: (12000.0 15000.0 18000.0 20000.0)

I have written the above codes, I want to add the first 2 values in the list and form a new list, but I think maybe it could be made shorter by using apply, mapcar or recursive method.
Could anybody help ?


EDIT (John): added code tags.

« Last Edit: November 17, 2020, 10:21:39 PM by John Kaul (Se7en) »

Tharwat

  • Swamp Rat
  • Posts: 556
  • Hypersensitive
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #1 on: November 18, 2020, 02:42:23 AM »
Hi,
Here is one way with foreach function.
Code - Auto/Visual Lisp: [Select]
  1. (defun Acum:Sum (m / a b r)
  2.   (setq a (car m))
  3.   (foreach b (cdr m)
  4.     (setq a (+ a b)
  5.           r (cons a r)
  6.     )
  7.   )
  8.   (reverse r)
  9. )
  10.  
  11. (Acum:Sum '(10000.0 2000.0 3000.0 3000.0 2000.0))

drawdraw

  • Mosquito
  • Posts: 4
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #2 on: November 18, 2020, 03:12:57 AM »
Great ! Tqvm !!!  :smitten:

Tharwat

  • Swamp Rat
  • Posts: 556
  • Hypersensitive
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #3 on: November 18, 2020, 03:22:14 AM »
Good to know.  :wink:

bruno_vdh

  • Mosquito
  • Posts: 7
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #4 on: November 18, 2020, 05:13:17 AM »
Hello, for exemple

mapcar method
Code: [Select]
(defun Acum:Sum (l / r)
  (setq r (car l))
  (mapcar '(lambda (x) (setq r (+ x r))) (cdr l))
)
recursive method
Code: [Select]
(defun Acum:Sum (l / rec)
  (defun rec (x l)
    (if l
      (cons (setq x (+ x (car l))) (rec x (cdr l)))
    )
  )
  (rec (car l) (cdr l))
)

drawdraw

  • Mosquito
  • Posts: 4
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #5 on: November 18, 2020, 11:53:04 PM »
Wow ! your codes is really amazing and inspiring ! thank you so much !!!   :smitten: :smitten:

Lee Mac

  • Seagull
  • Posts: 12411
  • London, England
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #6 on: November 19, 2020, 10:40:46 AM »
Just to provide some more variation:
Code - Auto/Visual Lisp: [Select]
  1. (defun f1 ( l ) (reverse (f2 (reverse l))))
  2. (defun f2 ( l ) (if (cdr l) (cons (apply '+ l) (f2 (cdr l)))))
Code - Auto/Visual Lisp: [Select]
  1. _$ (f1 '(10000.0 2000.0 3000.0 3000.0 2000.0))
  2. (12000.0 15000.0 18000.0 20000.0)

drawdraw

  • Mosquito
  • Posts: 4
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #7 on: November 19, 2020, 07:33:18 PM »
wow ! wow ! wow !  :smitten: :smitten: :smitten:
brain numbed !
Tqvm !

VovKa

  • Swamp Rat
  • Posts: 1282
  • Ukraine
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #8 on: November 19, 2020, 08:00:14 PM »
Code: [Select]
(defun f (l)
  (if (cdr l)
    (cons (+ (car l) (cadr l)) (f (cons (+ (car l) (cadr l)) (cddr l))))
  )
)

Lee Mac

  • Seagull
  • Posts: 12411
  • London, England
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #9 on: November 20, 2020, 06:55:50 AM »
Very good VovKa  :-)

bruno_vdh

  • Mosquito
  • Posts: 7
Re: Can make this codes shorter ? using apply, mapcar or recursive ?
« Reply #10 on: November 23, 2020, 04:48:33 AM »
Wow ! your codes is really amazing and inspiring ! thank you so much !!!   :smitten: :smitten:

Thanks, the codes that follow are too.
Kudos to VovKa for their code, which inspired me to make this slight optimization.
Code: [Select]
(defun f (l / x)
  (if (cdr l)
    (cons (setq x (+ (car l) (cadr l))) (f (cons x (cddr l))))
  )
)

or longer for those who don't like setq
Code: [Select]
(defun f (l)
  (if (cdr l)
    ((lambda (x) (cons x (f (cons x (cddr l)))))
      (+ (car l) (cadr l))
    )
  )
)