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

0 Members and 1 Guest are viewing this topic.

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.