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

0 Members and 1 Guest are viewing this topic.

RAIN CODE

  • Guest
how to insert new value into list
« on: June 20, 2012, 02:06:04 AM »
Hi guys,

How to write the shortest lisp using mapcar or lambda to insert a value into a list. Say I have 5 scores - (list 10000 9000 8000 7000 6000). And a new high score of 8500 to insert into this list, meaning the result is 10000 9000 8500 8000 7000. Always end with 5 highest score only, notice the last item in the list 6000 is removed from the list. I have written a lisp but its quite long. I wondered if using mapcar or lambda will shorten the lisp ??

Thanks

Daniel J. Ellis

  • Swamp Rat
  • Posts: 811
Re: how to insert new value into list
« Reply #1 on: June 20, 2012, 03:17:01 AM »
I'd probably do a FOREACH loop.

Load each item from the list in turn and compare it to the new value.  If the new value is the highest one replace the new value with the curent one and go to the next term.

dJE
===
dJE

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: how to insert new value into list
« Reply #2 on: June 20, 2012, 03:27:19 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun ins_item (lst new)
  2.   (if lst
  3.     (if
  4.       (>= new (car lst))
  5.        (cons new (reverse (cdr (reverse lst))))
  6.        (cons (car lst) (ins_item (cdr lst) new))
  7.     )
  8.   )
  9. )

kpblc

  • Bull Frog
  • Posts: 396
Re: how to insert new value into list
« Reply #3 on: June 20, 2012, 03:42:28 AM »
Another one:
Code - Auto/Visual Lisp: [Select]
  1. (defun test (/ lst value)
  2.  
  3.   (setq lst   (list 10000 9000 8000 7000 6000)
  4.         value 8500
  5.         ) ;_ end of setq
  6.   (vl-sort (cons value lst) '>)
  7.   ) ;_ end of defun
Sorry for my English.

pBe

  • Bull Frog
  • Posts: 402
Re: how to insert new value into list
« Reply #4 on: June 20, 2012, 03:49:37 AM »
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.       )

(topfive  '(10000 9000 8000 7000 6000) 8500)
(10000 9000 8500 8000 7000)

(topfive  '(10000 9000 8000 7000 6000) 5500)
(10000 9000 8000 7000 6000)

(topfive  '(10000 9000 8000 7000 6000) 11500)
(11500 10000 9000 8000 7000)

« Last Edit: June 20, 2012, 03:56:28 AM by pBe »

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: how to insert new value into list
« Reply #5 on: June 20, 2012, 04:00:47 AM »
kpblc and pBe
Code: [Select]
(setq lst '(10 8 8 8 6))

pBe

  • Bull Frog
  • Posts: 402
Re: how to insert new value into list
« Reply #6 on: June 20, 2012, 04:10:58 AM »
Busted   :-D

Code - Auto/Visual Lisp: [Select]
  1. (defun TopFive  (lst n / a b)
  2.       (repeat (length lst)
  3.             (if (>= (setq a (car lst)) n)
  4.                   (setq b   (append b (list a))
  5.                         lst (cdr lst))
  6.                   (setq b (append b (list n))
  7.                         n 0)))
  8.       b)
« Last Edit: June 20, 2012, 04:26:55 AM by pBe »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to insert new value into list
« Reply #7 on: June 20, 2012, 05:02:32 AM »
If AutoLisp had the same implementation as the common lisp "last" function this could have been written as:
Code - Auto/Visual Lisp: [Select]
  1. (reverse (last (vl-sort (cons value lst) '<) 5))
Otherwise something similar could be use from my code here: http://alisp-ext.wikidot.com/autolisp-firstn

E.g.
Code - Auto/Visual Lisp: [Select]
  1. (FirstN (vl-sort (cons value lst) '>) 5)
« Last Edit: June 20, 2012, 05:08:37 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to insert new value into list
« Reply #8 on: June 20, 2012, 05:12:48 AM »
BTW, is the list always only 5 items in length? I.e. you never have a list with more (or less) to begin with? If so your code could be:
Code - Auto/Visual Lisp: [Select]
  1. (reverse (cdr (vl-sort (cons value lst) '<)))
This would work for any length list ... keeping it the same length as it was by dropping the smallest value after adding the new value. Basically what pBe did - just ignoring the actual length.
« Last Edit: June 20, 2012, 05:15:57 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Faster

  • Guest
Re: how to insert new value into list
« Reply #9 on: June 20, 2012, 05:58:56 AM »
Code - Auto/Visual Lisp: [Select]
  1. (setq l '(10000 9000 8000 7000 6000) )
  2. (defun insertn (l n)
  3.   (if (> (car l) n)
  4.     (cons (car l) (insertn (cdr l) n))
  5.     (if l
  6.       (cons n (reverse (cdr (reverse l))))
  7.     ) ;_ if
  8.   ) ;_ if
  9. ) ;_ defun
  10. ;;test
  11. (insertn l 7500)
  12.  

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: how to insert new value into list
« Reply #10 on: June 20, 2012, 07:39:14 AM »
Iterative version of Stefan's:

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

Code - Auto/Visual Lisp: [Select]
  1. _$ (f 8500 '(10000 9000 8000 7000 6000))
  2. (10000 9000 8500 8000 7000)

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: how to insert new value into list
« Reply #11 on: June 20, 2012, 08:34:56 AM »
The 'mapcar way
Code - Auto/Visual Lisp: [Select]
  1. (defun ins_item_1 (l n)
  2.   (mapcar
  3.     '(lambda (x / m)
  4.        (if (>= n x) (setq m n n x x m))
  5.        x
  6.      )
  7.     l
  8.   )
  9. )

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: how to insert new value into list
« Reply #12 on: June 20, 2012, 08:59:57 AM »
Another  :lol:

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l n / g h )
  2.     (defun g ( a b ) (h a b))
  3.     (defun h ( a b ) (if (< n a) a (progn (defun h ( a b ) b) n)))
  4.     (mapcar 'g l (cons nil l))
  5. )

RAIN CODE

  • Guest
Re: how to insert new value into list
« Reply #13 on: June 20, 2012, 09:21:16 AM »
wow ! you guys are pretty good. Amazing. All the suggestions here are shorter than my lisp.

to irneb - yes.. the list is always 5 in length. Well maybe I will make it 10 (top ten hi score).

I will post the game here once it finished.  The most used function in the game is the one suggested by CAB about 2 weeks ago, to replace old list with new value. It took me an hour or two to understand that function with mapcar and lambda.  Its so amazingly short, its like Einstein's famous equation E=mc sq  ^-^

Thanks guys - you all are the greatest.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: how to insert new value into list
« Reply #14 on: June 20, 2012, 09:34:15 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))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: how to insert new value into list
« Reply #15 on: June 20, 2012, 10:39:49 AM »
recursion:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n)
  2.   (cond ((not (cdr l)) (list (min (car l) n)))
  3.         ((> (car l) n) (cons (car l) (f (cdr l) n)))
  4.         ((cons n (f (cdr l) (car l))))
  5.   )
  6. )

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: how to insert new value into list
« Reply #16 on: June 20, 2012, 10:44:44 AM »
Not quite...

Code - Auto/Visual Lisp: [Select]
  1. _$ (f '(6 5 4 3 2) 1)
  2. (6 5 4 3 1)

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l n )
  2.     (cond ((not (cdr l)) (list (max (car l) n)))
  3.           ((> (car l) n) (cons (car l) (f (cdr l) n)))
  4.           ((cons n (f (cdr l) (car l))))
  5.     )
  6. )

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: how to insert new value into list
« Reply #17 on: June 20, 2012, 10:48:08 AM »
Not quite...

Code - Auto/Visual Lisp: [Select]
  1. _$ (f '(6 5 4 3 2) 1)
  2. (6 5 4 3 1)

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l n )
  2.     (cond ((not (cdr l)) (list (max (car l) n)))
  3.           ((> (car l) n) (cons (car l) (f (cdr l) n)))
  4.           ((cons n (f (cdr l) (car l))))
  5.     )
  6. )
Yes, did not work... :)
 time to rest!

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: how to insert new value into list
« Reply #18 on: June 20, 2012, 10:50:39 AM »
I do applaud your concise solution(s) however  :-)

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: how to insert new value into list
« Reply #19 on: June 20, 2012, 01:55:30 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f (x n / a)
  2.   (cond
  3.     ( (> (setq a (car x)) n)
  4.       (cons a (f (cdr x) n))
  5.     )
  6.     ( a
  7.       (cons n (reverse (cdr (reverse x))))
  8.     )
  9.   )
  10. )
  11.  
;;

_$ (f '(9 8 7 6 5 3) 1)
(9 8 7 6 5 3)
_$ (f '(9 8 7 6 5 3) 10)
(10 9 8 7 6 5)
_$ (f '(9 8 7 6 5 3) 3)
(9 8 7 6 5 3)
_$ (f '(9 8 7 6 5 3) 5)
(9 8 7 6 5 5)
_$ (f '(9 8 7 6 5 3) 4)
(9 8 7 6 5 4)
_$ (f '(9 8 7 6 5 3) 9)
(9 9 8 7 6 5)
I am a bilingualist,Chinese and Chinglish.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: how to insert new value into list
« Reply #20 on: June 20, 2012, 02:09:00 PM »
Nice one HighflyingBird, the 'cond' version of Stefan's  :-)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: how to insert new value into list
« Reply #21 on: June 20, 2012, 02:43:58 PM »
Not quite...

Code - Auto/Visual Lisp: [Select]
  1. _$ (f '(6 5 4 3 2) 1)
  2. (6 5 4 3 1)

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l n )
  2.     (cond ((not (cdr l)) (list (max (car l) n)))
  3.           ((> (car l) n) (cons (car l) (f (cdr l) n)))
  4.           ((cons n (f (cdr l) (car l))))
  5.     )
  6. )
Yes, did not work... :)
 time to rest!
My test:
Code: [Select]
(f '(6 5 4 3 2) 1)    ; (6 5 4 3 2)
(f '(9 8 7 6 5 3) 1)  ; (9 8 7 6 5 3)
(f '(9 8 7 6 5 3) 10) ; (10 9 8 7 6 5)
(f '(9 8 7 6 5 3) 3)  ; (9 8 7 6 5 3)
(f '(9 8 7 6 5 3) 5)  ; (9 8 7 6 5 5)
(f '(9 8 7 6 5 3) 4)  ; (9 8 7 6 5 4)
(f '(9 8 7 6 5 3) 9)  ; (9 9 8 7 6 5)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: how to insert new value into list
« Reply #22 on: June 21, 2012, 09:44:21 AM »
new variant:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n)
  2.   (cond ((and l (< n (car l))) (cons (car l) (f (cdr l) n)))
  3.         (l (cons n (f (cdr l) (car l))))
  4.   )
  5. )

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: how to insert new value into list
« Reply #23 on: June 21, 2012, 10:08:21 AM »
new variant 2:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n) (if l (cons (max n (car l)) (f (cdr l) (min n (car l))))))
;-)

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: how to insert new value into list
« Reply #24 on: June 21, 2012, 10:25:42 AM »
new variant:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n)
  2.   (cond ((and l (< n (car l))) (cons (car l) (f (cdr l) n)))
  3.         (l (cons n (f (cdr l) (car l))))
  4.   )
  5. )

 8-),so beautiful!
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n)
  2.   (cond
  3.     ( (> (car l) n)(cons (car l) (f (cdr l) n)))
  4.     ( l (cons n (f (cdr l) (car l))))
  5.   )
  6. )
  7.  
  ---
A function,includes the important things of LISP: four  basic LISP operations: cons cond car cdr
and recursion!
« Last Edit: June 21, 2012, 11:00:26 AM by HighflyingBird »
I am a bilingualist,Chinese and Chinglish.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: how to insert new value into list
« Reply #25 on: June 21, 2012, 11:01:04 AM »
...
 8-),so beautiful!
...
A function,includes the important : four  basic LISP operations: cons cond car cdr
and recursion!

Thank you!
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. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to insert new value into list
« Reply #26 on: June 21, 2012, 11:36:07 AM »
Just be careful with min/max with mixed integer/real datatypes. It's going to propagate reals instead of integers if there's any one real in the list.

I like the one you did in #23 the most ... order N to perform the whole thing. Just sorry it's recursive - so no long lists for that one.

BTW, I'm not sure if something similar has been done in a previous post:
Code - Auto/Visual Lisp: [Select]
  1. (defun InsVal  (lst val / n f)
  2.   (defun f (comp) (apply 'append (mapcar (function (lambda (item) (cond ((comp item val) (list item))))) lst)))
  3.   (setq n (length lst))
  4.          (mapcar (function (lambda (item) (cond ((> (setq n (1- n)) -1) (list item)))))
  5.                  (append (f >=) (list val) (f <)))))

And AFAICT the "easiest" way to check if the list is already sorted:
Code - Auto/Visual Lisp: [Select]
  1.   (if (not (apply '>= lst))
  2.      (setq lst (vl-sort lst (function >))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

xiaxiang

  • Guest
Re: how to insert new value into list
« Reply #27 on: June 21, 2012, 11:57:01 AM »
Hi,guys
wonderful anwsers!

chlh_jd

  • Guest
Re: how to insert new value into list
« Reply #28 on: June 21, 2012, 12:04:53 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)
(f nil 1)                  ; (1)
« Last Edit: June 21, 2012, 12:09:00 PM by chlh_jd »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to insert new value into list
« Reply #29 on: June 21, 2012, 12:24:44 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.

Strangely enough, the reverse function doesn't seem to be all that slow. I've just tried re-imlpementing my ButLastN function by using the same principle as my previous post.
Code - Auto/Visual Lisp: [Select]
  1. (defun butlastN1  (lst n / m)
  2.   (setq m (- (length lst) n))
  3.   (mapcar 'car
  4.           (apply 'append
  5.                  (mapcar (function (lambda (item) (cond ((>= (setq m (1- m)) 0) (list (list item)))))) lst))))
But it seems to actually run slower than the old one which reversed the list twice:
Code: [Select]
_$ (QuickBench '((ButLastN lst 100) (ButLastN1 lst 100)))
Benchmarking .. done for 4096 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(BUTLASTN LST 100)                            4096      1950      1950      5.51
(BUTLASTN1 LST 100)                            512      1342     10736      1.00
--------------------------------------------------------------------------------
_$ (QuickBench '((ButLastN lst 500) (ButLastN1 lst 500)))
Benchmarking .. done for 2048 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(BUTLASTN LST 500)                            2048      1498      1498      2.96
(BUTLASTN1 LST 500)                            512      1107      4428      1.00
--------------------------------------------------------------------------------
_$ (QuickBench '((ButLastN lst 900) (ButLastN1 lst 900)))
Benchmarking .. done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(BUTLASTN LST 900)                            1024      1044      1044      1.66
(BUTLASTN1 LST 900)                           1024      1732      1732      1.00
--------------------------------------------------------------------------------
_$ (QuickBench '((ButLastN lst 950) (ButLastN1 lst 950)))
Benchmarking .. done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(BUTLASTN LST 950)                            1024      1060      1060      1.59
(BUTLASTN1 LST 950)                           1024      1685      1685      1.00
--------------------------------------------------------------------------------
_$ (QuickBench '((ButLastN lst 990) (ButLastN1 lst 990)))
Benchmarking .. done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(BUTLASTN LST 990)                            1024      1107      1107      1.47
(BUTLASTN1 LST 990)                           1024      1622      1622      1.00
--------------------------------------------------------------------------------
Run on a sample list of 1000 integers.
« Last Edit: June 21, 2012, 12:28:18 PM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

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: 12913
  • 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: 12913
  • 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: 12913
  • 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.