Author Topic: Compress for list  (Read 1327 times)

0 Members and 1 Guest are viewing this topic.

sena

  • Mosquito
  • Posts: 2
Compress for list
« on: May 06, 2017, 12:02:02 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun ::Compress(lst construct / cons_list $N DB W)
  2.     (defun cons_list()
  3.         (if construct
  4.             (cond((= w 1)(cons $n db))((> w 0)(cons (cons $n w) db)))
  5.             (if(> w 0)(cons w (cons $n db)))
  6.         )
  7.     )
  8.  
  9.     (setq $n nil w 0 db nil)
  10.     (foreach n lst
  11.         (if (/= $n n)(setq db (cons_list) $n n w 0))
  12.         (setq w (1+ w))
  13.     )
  14.     (setq db (cons_list))
  15.     (reverse db)
  16. )
  17.  
  18.  
  19. example:
  20.  
  21. _$ (::Compress  '(1 1 1 2 2 2 3 3 3 3 3 3 4 4 4 4 5 6 6 6 7 8) T)
  22. ((1 . 3) (2 . 3) (3 . 6) (4 . 4) 5 (6 . 3) 7 8)
  23. _$ (::Compress  '(1 1 1 2 2 2 3 3 3 3 3 3 4 4 4 4 5 6 6 6 7 8) F)
  24. (1 3 2 3 3 6 4 4 5 1 6 3 7 1 8 1)
  25.  
  26.  
« Last Edit: May 06, 2017, 12:06:02 AM by sena »

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Compress for list
« Reply #1 on: May 06, 2017, 10:07:38 AM »
Some alternatives: Count Items

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Compress for list
« Reply #2 on: May 06, 2017, 06:11:35 PM »
Neat function, Lee!
Its hard to find LM:Countitems due the "Unique & Duplicate List Functions" title.
I'll always remember this TallyHo assembly by M.Puckett.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Compress for list
« Reply #3 on: May 06, 2017, 06:14:16 PM »
Thanks Grrr  :-)