Author Topic: [challenge] A27 : delete-if / delete-if-not  (Read 1578 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10158
Re: [challenge] A27 : delete-if / delete-if-not
« Reply #15 on: February 24, 2022, 03:13:12 PM »
@pBe
I just noticed your latest edit now. Your latest edit code looks good (looks to meet the specifications).
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

pBe

  • Bull Frog
  • Posts: 401
Re: [challenge] A27 : delete-if / delete-if-not
« Reply #16 on: February 24, 2022, 08:39:54 PM »
@pBe
I just noticed your latest edit now. Your latest edit code looks good (looks to meet the specifications).

Thank you for that @JohnK, it did not make the cut but at least it meets the specifications :)
I will read it thoroughly next time.


dexus

  • Newt
  • Posts: 79
Re: [challenge] A27 : delete-if / delete-if-not
« Reply #17 on: March 24, 2022, 03:54:01 AM »
Hi,
To avoid calling apply (or eval) at each loop, it would be more efficient to set f to (eval f) before the loops and simply call (f item) at each loop.
Code - Auto/Visual Lisp: [Select]
  1. (defun deleteif (f l / loop)
  2.   (defun loop (f l)
  3.     (cond
  4.       ((not l) l)
  5.       ((f (car l)) (deleteif f (cdr l)))
  6.       ((cons (car l) (deleteif f (cdr l))))
  7.     )
  8.   )
  9.   (loop (eval f) l)
  10. )
Wait, shouldn't be the loop function that does the recursion instead of the deleteif? This way it still does an eval every loop. :wink:

If you create a new function anyway, you don't even have to pass it along as a variable.
Just create the variable in the parent function and use that, I'm not sure if that is more efficient though.
Code - Auto/Visual Lisp: [Select]
  1. (defun deleteif (f l / loop)
  2.   (setq f (eval f))
  3.   (defun loop (l)
  4.     (cond
  5.       ((not l) l)
  6.       ((f (car l)) (loop (cdr l)))
  7.       ((cons (car l) (loop (cdr l))))
  8.     )
  9.   )
  10.   (loop l)
  11. )