Author Topic: -={ Challenge }=- SubStr for Lists  (Read 11671 times)

0 Members and 2 Guests are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
-={ Challenge }=- SubStr for Lists
« on: October 01, 2010, 03:11:11 PM »
The Challenge:

To construct a function to return a section of a list, with behaviour akin to the substr function (with the exception of a zero-based index)

Code: [Select]
(SubList <list> <start> [<length>])
Examples:

Code: [Select]
(SubList '(0 1 2 3 4 5) 2 3)
>> (2 3 4)

Code: [Select]
(SubList '(0 1 2 3 4 5) 2 nil)
>> (2 3 4 5)

Code: [Select]
(SubList '(0 1 2 3 4 5) 6 3)
>> nil


T.Willey

  • Needs a day job
  • Posts: 5251
Re: -={ Challenge }=- SubStr for Lists
« Reply #1 on: October 01, 2010, 03:49:54 PM »
Code: [Select]
(defun SubList ( lst st len )
   
    (repeat st
        (setq lst (cdr lst))
    )
    (if
        (or
            (not len)
            (zerop len)
        )
        lst
        (reverse (SubList (reverse lst) (- (length lst) len) 0))
    )
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- SubStr for Lists
« Reply #2 on: October 01, 2010, 03:50:13 PM »
Hi,

this one i posted here:

Code: [Select]
;; SUBLIST
;; Returns a sub list
;;
;; Arguments
;; lst : a list
;; start : start index (first item = 0)
;; leng : the sub list length (number of items) or nil

(defun sublist (lst start leng / n r)
  (if (or (not leng) (< (- (length lst) start) leng))
    (setq leng (- (length lst) start))
  )
  (setq n (+ start leng))
  (while (< start n)
    (setq r (cons (nth (setq n (1- n)) lst) r))
  )
)

Or a recursive form:

Code: [Select]
;; SUBLST
;; Returns a sub list
;;
;; Arguments
;; lst : a list
;; start : start index (first item = 0)
;; leng : the sub list length (number of items) or nil

(defun sublst (lst start leng)
  (or leng (setq leng (length lst)))
  (if (and lst (< 0 leng))
    (if    (zerop start)
      (cons (car lst) (sublst (cdr lst) 0 (1- leng)))
      (sublst (cdr lst) (1- start) leng)
    )
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #3 on: October 01, 2010, 03:52:23 PM »
Nice one guys!, some interesting results  :-)

Here was mine:

Code: [Select]
(defun LM:SubList ( lst start len )
  (if lst
    (if (< 0 start)
      (LM:SubList (cdr lst) (1- start) len)
      (if len
        (if (< 0 len)
          (cons (car lst) (LM:SubList (cdr lst) start (1- len)))
        )
        lst
      )
    )
  )
)

Similar to Giles I think

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- SubStr for Lists
« Reply #4 on: October 01, 2010, 03:53:33 PM »
Code: [Select]
(defun AT:SubList (lst left lgth / _trim)

  (defun _trim (l i)
    (if (> i 0)
      (_trim (cdr l) (1- i))
      l
    )
  )

  (if lgth
    (reverse (_trim (reverse (_trim lst left)) (- (length lst) left lgth)))
    (_trim lst left)
  )
)
« Last Edit: October 03, 2010, 09:18:37 AM by alanjt »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

T.Willey

  • Needs a day job
  • Posts: 5251
Re: -={ Challenge }=- SubStr for Lists
« Reply #5 on: October 01, 2010, 04:11:47 PM »
In case the list is long.
Code: [Select]
(defun SubList ( lst st len / tempList )
   
    (setq len
        (if (not len)
            (length lst)
            (1- (+ st len))
        )
    )
    (vl-catch-all-apply
        (function
            (lambda ( / cnt )
                (setq cnt 0)
                (foreach i lst
                    (cond
                        ((<= st cnt len)
                            (setq tempList (cons i tempList))
                        )
                        ((> cnt len)
                            (exit)
                        )
                    )
                    (setq cnt (1+ cnt))
                )
            )
        )
    )
    (reverse tempList)
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #6 on: October 01, 2010, 04:28:34 PM »
Here's one that was prepared earlier

Similar to most ..
Code - Auto/Visual Lisp: [Select]
  1. ;;;-----------------------------------------------------------------------------------
  2. ;;;-----------------------------------------------------------------------------------
  3. ;;;
  4. ;;; KDUB:sublist Return a sub-list
  5. ;;;
  6. ;;; Arguments
  7. ;;; lst : a list
  8. ;;; start : start index for the sub-list (first item = 0)
  9. ;;; leng : sub-list length (or nil)
  10. ;;;
  11. ;;; Examples :
  12. ;;; (KDUB:sublist '(1 2 3 4 5 6) 2 2) -> (3 4)
  13. ;;; (KDUB:sublist '(1 2 3 4 5 6) 2 nil) -> (3 4 5 6)
  14.  
  15. (defun KDUB:sublist (lst start leng / n r)
  16.   (if (or (not leng) (< (- (length lst) start) leng))
  17.     (setq leng (- (length lst) start))
  18.   )
  19.   (setq n (+ start leng))
  20.   (repeat leng (setq r (cons (nth (setq n (1- n)) lst) r)))
  21. )
« Last Edit: September 02, 2012, 08:43:15 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #7 on: October 01, 2010, 04:34:09 PM »
Interesting ::

Excluding Tim's routine for 'Long lists'
There is a 46% range difference in speed for those posts  :?

using
(setq TestList '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30))
(doit:sublist TestList 9 13)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: -={ Challenge }=- SubStr for Lists
« Reply #8 on: October 01, 2010, 04:43:00 PM »
This one shouldn't beat any speed records but I wanted to be different.

Code: [Select]
(defun sublist ( lst start len / iterations)
    (defun sublist-iter (sofar lis)
       (cond
         ((or (null lis) (eq (length sofar) len))
             (reverse sofar) )
         ((>= iterations start)
    (setq iterations (1+ iterations))
             (sublist-iter
     (cons (car lis) sofar)
              (cdr lis)))
         ( T (setq iterations (1+ iterations))
    (sublist-iter '() (cdr lis)))) )
   (setq iterations 0)
   (sublist-iter '() lst)
 )


EDIT: Found a mistake (extra code) and fixed it. It will still operate as was before.
« Last Edit: October 01, 2010, 05:20:22 PM by Se7en »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #9 on: October 01, 2010, 05:00:54 PM »
Another, iterative version...

Code: [Select]
(defun LM:SubList-iter ( lst start len / r )
  (or len (setq len (- (length lst) start)))
 
  (repeat start (setq lst (cdr lst)))
 
  (setq i -1)

  (while (and lst (< (setq i (1+ i)) len))
    (setq r (cons (car lst) r) lst (cdr lst))
  )
  (reverse r)
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- SubStr for Lists
« Reply #10 on: October 01, 2010, 05:01:13 PM »
three sketches...
Code: [Select]
(defun f (l s n)
 (cond ((not l) nil)
       ((> s 0) (f (cdr l) (1- s) n))
       ((not n) l)
       ((> n 0) (cons (car l) (f (cdr l) 0 (1- n))))
 )
)
Code: [Select]
(defun f1 (l s n / i)
 (setq i -1
       n (if n (+ s n i) (length l))
 )
 (vl-remove-if-not (function (lambda (a) (<= s (setq i (1+ i)) n))) l)
)
Code: [Select]
(defun f2 (l s n)
 (repeat (/ s 4)
  (setq l (cddddr l))
 )
 (repeat (rem s 4)
  (setq l (cdr l))
  )
 
 (setq s nil)
 (if (and l n)
  (progn (repeat (/ n 4)
          (setq s (cons (car l) (cons (cadr l) (cons (caddr l) (cons (cadddr l) s))))
                l (cddddr l)
          )
         )
         (repeat (rem n 4)
                   (setq s (cons (car l) s)
                         l (cdr l)
                   )
                  )
         (reverse s)
  )
  l
 )
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- SubStr for Lists
« Reply #11 on: October 01, 2010, 05:17:31 PM »
Nice coding Evgeniy  8-)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- SubStr for Lists
« Reply #12 on: October 01, 2010, 05:18:18 PM »
 :oops:
Code: [Select]
(defun f2 (l s n)
 (repeat (/ s 4)
  (setq l (cddddr l))
 )
 (repeat (rem s 4)
  (setq l (cdr l))
  )
 
 (setq s nil)
 (if (and l n)
  (progn (repeat (/ n 4)
          (setq s (cons [color=red](cadddr l) (cons (caddr l) (cons (cadr l) (cons (car l)[/color] s))))
                l (cddddr l)
          )
         )
         (repeat (rem n 4)
                   (setq s (cons (car l) s)
                         l (cdr l)
                   )
                  )
         (reverse s)
  )
  l
 )
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- SubStr for Lists
« Reply #13 on: October 01, 2010, 05:21:46 PM »
Moscow now has one in the morning.
I go to bed...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- SubStr for Lists
« Reply #14 on: October 01, 2010, 05:33:46 PM »
using
(setq TestList '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30))

Haven't tested with any varying length Lists

The range is about 10% to 50% difference
Code: [Select]

Benchmarking [M.P. 2005 < revised kdub 2005>] ..................
Elapsed milliseconds for 32768 iteration(s)/ relative Timing :

    (TW:L-SUBLIST TESTLIST 9 13)........2886 / 2.4987 <slowest>
    (J7:SUBLIST TESTLIST 9 13)..........1997 / 1.7290
    (EE:F TESTLIST 9 13)................1747 / 1.5126
    (GILE:SUBLST TESTLIST 9 13).........1700 / 1.4719
    (LM:SUBLIST TESTLIST 9 13)..........1607 / 1.3913
    (LM:SUBLIST-ITER TESTLIST 9 13).....1451 / 1.2563
    (EE:F1 TESTLIST 9 13)...............1404 / 1.2156
    (EE:F2 TESTLIST 9 13)...............1311 / 1.1351
    (AT:SUBLIST TESTLIST 9 13)..........1311 / 1.1351
    (GILE:SUBLIST TESTLIST 9 13)........1310 / 1.1342
    (TW:SUBLIST TESTLIST 9 13)..........1279 / 1.1074
    (KDUB:SUBLIST TESTLIST 9 13)........1155 / 1.0000 <fastest>

 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.