Author Topic: how to insert new value into list  (Read 10795 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: 12914
  • 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: 12914
  • 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))