Author Topic: how to insert new value into list  (Read 10785 times)

0 Members and 1 Guest are viewing this topic.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: how to insert new value into list
« Reply #30 on: June 21, 2012, 12:27:01 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n)
  2.   (if l
  3.     (cons (max n (car l)) (f (cdr l) (min n (car l))))
  4.   )
  5. )
Wow.. Evgeniy, that is a really beauty piece of code...
I think this thread deserve a "challenge" in front of it..
If some of you will 'benchmark' this, I want to refine my variants:
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:f1 (l n)
  2.      (if l
  3.         (if
  4.          (> n (car l))
  5.           (cons n (reverse (cdr (reverse l))))
  6.           (cons (car l) (ph:f1 (cdr l) n))
  7.        )
  8.      )
  9.     )
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:f2 (l n)
  2.      (mapcar
  3.        (function
  4.           (lambda (x / m)
  5.             (if (> n x) (setq m n n x x m) x)
  6.           )
  7.         )
  8.       l
  9.     )
  10.   )
« Last Edit: June 21, 2012, 03:25:51 PM by Stefan »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: how to insert new value into list
« Reply #31 on: June 21, 2012, 12:34:37 PM »
Not for duplicates...

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:f1 ( l n ) (reverse (cdr (vl-sort (cons n l) '<))))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to insert new value into list
« Reply #32 on: June 21, 2012, 01:06:25 PM »
Consolidated those which do what the OP asked for. Attached.

Tested each using the TestFunc in attached. Didn't consider duplicates though.

Here's the benchmark tests on the OP's original list:
Code: [Select]
Benchmarking ............... done for 32768 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(FASTER:INSERTN (QUOTE (10000 9000 8...)     32768      1419      1419      4.93
(PBE:TOPFIVE (QUOTE (10000 9000 8000...)     32768      1481      1481      4.72
(IB:INS-KEEP-LENGTH (QUOTE (10000 90...)     32768      1483      1483      4.71
(LM:F (QUOTE (10000 9000 8000 7000 6...)     32768      1497      1497      4.67
(HB:F (QUOTE (10000 9000 8000 7000 6...)     32768      1513      1513      4.62
(LM:F2 (QUOTE (10000 9000 8000 7000 ...)     32768      1607      1607      4.35
(STEFAN:INS_ITEM_1 (QUOTE (10000 900...)     32768      1621      1621      4.31
(EE:F1 (QUOTE (10000 9000 8000 7000 ...)     32768      1670      1670      4.19
(STEFAN:INS_ITEM (QUOTE (10000 9000 ...)     32768      1701      1701      4.11
(EE:F2 (QUOTE (10000 9000 8000 7000 ...)     32768      1701      1701      4.11
(PBE:TOPFIVE1 (QUOTE (10000 9000 800...)     32768      1824      1824      3.83
(LM:F1 (QUOTE (10000 9000 8000 7000 ...)     32768      1841      1841      3.80
(EE:F (QUOTE (10000 9000 8000 7000 6...)     32768      2012      2012      3.48
(HB:F1 (QUOTE (10000 9000 8000 7000 ...)     16384      1060      2120      3.30
(IB:INSVAL (QUOTE (10000 9000 8000 7...)      8192      1748      6992      1.00
--------------------------------------------------------------------------------
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: how to insert new value into list
« Reply #33 on: June 21, 2012, 01:20:26 PM »
Not for duplicates...

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:f1 ( l n ) (reverse (cdr (vl-sort (cons n l) '<))))

Oops! I see that my LM:F1 function above is pretty much the same as pBe's TOPFIVE code...

Code - Auto/Visual Lisp: [Select]
  1. (defun TopFive  (lst n)
  2.       (if (>  n (last lst))
  3.             (reverse
  4.                   (cdr (vl-sort
  5.                              (cons n lst)
  6.                              '<)))
  7.             lst)
  8.       )

chlh_jd

  • Guest
Re: how to insert new value into list
« Reply #34 on: June 21, 2012, 03:10:35 PM »
ALL NICE !
Code: [Select]
(defun f (L n)
  (if (> n (car l)) (cons n l)
     (cons (car l) (f (cdr l) n))))
Test
Code: [Select]
(f '(6 5 4 3 2) 1)    ; (6 5 4 3 2 1)
(f '(9 8 7 6 5 3) 1)  ; (9 8 7 6 5 3 1)
(f '(9 8 7 6 5 3) 10) ; (10 9 8 7 6 5 3)
(f '(9 8 7 6 5 3) 3)  ; (9 8 7 6 5 3 3)
(f '(9 8 7 6 5 3) 5)  ; (9 8 7 6 5 5 3)
(f '(9 8 7 6 5 3) 4)  ; (9 8 7 6 5 4 3)
(f '(9 8 7 6 5 3) 9)  ; (9 9 8 7 6 5 3)
Not exactly what the OP asked for. He wanted the list to stay the same length by dropping the smallest value after inserting the new value.
I'm sorry to do wrong the mean .
Code: [Select]
(defun f (l n / f1)
  (defun f1 (L n)
    (if (< n (car l))
      (cons n l)
      (cons (car l) (f1 (cdr l) n))
    )
  )
  (reverse (cdr (f1 (reverse l) n)))
)
My Test result
测试耗时......毫秒 / 相对速度 ...... 迭代次数32768...:
 PH:F1......60 / 15.34......<Fastest>
 LM:F1......160 / 5.75......
 PH:F2......220 / 4.18......
 F......230 / 4.00......
 HB:F......260 / 3.54......
 EA:F......260 / 3.54......
 INSVAL......920 / 1.00......<Slowest>

chlh_jd

  • Guest
Re: how to insert new value into list
« Reply #35 on: June 21, 2012, 03:16:02 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:f1 (l n)
  2.      (if lst
  3.        (if
  4.          (> n (car l))
  5.           (cons n (reverse (cdr (reverse l))))
  6.           (cons (car l) (ph:f1 (cdr l) n))
  7.        )
  8.      )
  9.     )
Here's
Code: [Select]
(if lst must be  '(if L'?
New Test result
测试耗时......毫秒 / 相对速度 ...... 迭代次数32768...:
 LM:F1......160 / 5.81......<Fastest>
 PH:F1......160 / 5.81......
 PH:F2......210 / 4.43......
 F......220 / 4.23......
 EA:F......260 / 3.58......
 HB:F......260 / 3.58......
 INSVAL......930 / 1.00......<Slowest>


Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: how to insert new value into list
« Reply #36 on: June 21, 2012, 03:22:34 PM »
Here's
Code: [Select]
(if lst must be  '(if L'?
Edited "in place" and missed that one... sorry.

pBe

  • Bull Frog
  • Posts: 402
Re: how to insert new value into list
« Reply #37 on: June 22, 2012, 01:50:44 AM »
I prefer:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n)
  2.   (if l
  3.     (cons (max n (car l)) (f (cdr l) (min n (car l))))
  4.   )
  5. )

Incredible. What are they feeding you...  Guess i need to change my diet  :-D

pBe

  • Bull Frog
  • Posts: 402
Re: how to insert new value into list
« Reply #38 on: June 22, 2012, 02:13:31 AM »
(IB:INSVAL (QUOTE (10000 9000 8000 7...)     

Which  one is this Irné ?

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to insert new value into list
« Reply #39 on: June 22, 2012, 03:53:48 AM »
Which  one is this Irné ?
That's the one from my post #27. The IB is my initials  ;) .

BTW: The IB:INS-KEEP-LENGTH is actually not mine but yours (post #5) - just without the if statement as I've noted in post #9. Though I think I got a bit confused by your 2 versions  :ugly: . It should've read: "Basically what pBe did, just ignoring the check for new value smaller than last". Which is why it's so close to yours in the test, yours would be faster if the benchmark considered adding a value smaller than all the rest.

Edit: I'm guessing the timings would start changing drastically as the list sent is larger. But since the list is only 5 items, the usual suspects don't really apply. I'm actually surprised that Elpanov's don't surpass the rest - but that's probably due to the list-length.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: how to insert new value into list
« Reply #40 on: June 22, 2012, 04:56:33 AM »
Only the love of arts...  :-)
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n) (mapcar 'max (mapcar '(lambda (a) (min a n)) (cons n l)) l))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: how to insert new value into list
« Reply #41 on: June 22, 2012, 08:00:26 AM »
Very original Evgeniy  :-)

A shortened version of my earlier iterative code:

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:f2 ( l n / a o )
  2.     (while (< n (setq a (car l)))
  3.         (setq o (cons a o)
  4.               l (cdr l)
  5.         )
  6.     )
  7.     (reverse (append (cdr (reverse (cons n l))) o))
  8. )

bruno_vdh

  • Guest
Re: how to insert new value into list
« Reply #42 on: June 22, 2012, 09:24:33 AM »
Hello,

My variant with mapcar + lambda 
Code: [Select]
(defun bv:f1 (l n / flag)
  (mapcar '(lambda (a b) (if flag b (if (> n a) (setq flag n) a))) l (cons (car l) l))
)

The same for those who prefer cond to if
Code: [Select]
(defun bv:f2 (l n / flag)
  (mapcar '(lambda (a b) (cond (flag b) ((> n a) (setq flag n)) (a))) l (cons (car l) l))
)

Excuse my English, I speak with Google Translation
A+

RAIN CODE

  • Guest
Re: how to insert new value into list
« Reply #43 on: June 26, 2012, 01:56:17 AM »
another 2

Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n) (mapcar '(lambda (a b) (nth a (cons n l))) (vl-sort-i (cons n l) '>) l))

You write very good program. I wish I could write like you do.

Thanks for your help.  :-)

You are one of the best in this swamp forum - no doubt about it.