(setq lst '(10 8 8 8 6))
Not quite...Yes, did not work... :)Code - Auto/Visual Lisp: [Select]
_$ (f '(6 5 4 3 2) 1) (6 5 4 3 1)Code - Auto/Visual Lisp: [Select]
My test:Not quite...Yes, did not work... :)Code - Auto/Visual Lisp: [Select]
_$ (f '(6 5 4 3 2) 1) (6 5 4 3 1)Code - Auto/Visual Lisp: [Select]
time to rest!
(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)
new variant:Code - Auto/Visual Lisp: [Select]
...
8-),so beautiful!
...
A function,includes the important : four basic LISP operations: cons cond car cdr
and recursion!
(defun f (L n)
(if (> n (car l)) (cons n l)
(cons (car l) (f (cdr l) n))))
Test(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)
ALL NICE !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.Code: [Select](defun f (L n)
Test
(if (> n (car l)) (cons n l)
(cons (car l) (f (cdr l) n))))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)
_$ (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.
Wow.. Evgeniy, that is a really beauty piece of code...Code - Auto/Visual Lisp: [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
--------------------------------------------------------------------------------
Not for duplicates...Code - Auto/Visual Lisp: [Select]
Code - Auto/Visual Lisp: [Select]
I'm sorry to do wrong the mean .ALL NICE !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.Code: [Select](defun f (L n)
Test
(if (> n (car l)) (cons n l)
(cons (car l) (f (cdr l) n))))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)
(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 Here'sCode - Auto/Visual Lisp: [Select]
(if lst
must be '(if L'?Here'sEdited "in place" and missed that one... sorry.Code: [Select](if lst
must be '(if L'?
I prefer:Code - Auto/Visual Lisp: [Select]
(IB:INSVAL (QUOTE (10000 9000 8000 7...)
Which one is this Irné ?That's the one from my post #27. The IB is my initials ;) .
(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))
)
(defun bv:f2 (l n / flag)
(mapcar '(lambda (a b) (cond (flag b) ((> n a) (setq flag n)) (a))) l (cons (car l) l))
)
another 2Code - Auto/Visual Lisp: [Select]