Author Topic: (cdddddddddddddddr Lst)  (Read 4443 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
(cdddddddddddddddr Lst)
« on: November 26, 2016, 03:35:25 PM »
Hi guys,
Any idea how to write such function that returns a list starting from nth x position ?
Code: [Select]
Lst ; a 1
(cdr Lst) ; b 2
(cddr Lst) ; c 3
(cdddr Lst) ; d 4
(cddddr Lst) ; e 5
Code: [Select]
(cdddddr Lst) ; f 6 ???
(cddddddr Lst) ; g 7 ???
                ...
My brain is stuck today.
EDIT:
Figured something with an old function I had:
Code: [Select]
_$ (defun ShiftListRightRepeat ( n Lst / nLst )
(if (and (= 'INT (type n)) (listp Lst))
(progn
(setq nLst Lst)
(repeat n (setq nLst (append (cdr nLst) (list (car nLst)))))
)
)
nLst
); defun ShiftListRightRepeat
SHIFTLISTRIGHTREPEAT
_$ (setq Lst (mapcar 'chr (vl-string->list "ABCDEFGHIJKLMNOPQ")))
("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q")
_$ (ShiftListRightRepeat 1 Lst)
("B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "A")
_$ (ShiftListRightRepeat 2 Lst)
("C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "A" "B")
_$ (ShiftListRightRepeat 3 Lst)
("D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "A" "B" "C")
_$ (ShiftListRightRepeat 4 Lst)
("E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "A" "B" "C" "D")
_$ (ShiftListRightRepeat 5 Lst)
("F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "A" "B" "C" "D" "E")
_$ 
« Last Edit: November 26, 2016, 03:49:11 PM by Grrr1337 »
(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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (cdddddddddddddddr Lst)
« Reply #1 on: November 26, 2016, 03:54:42 PM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (cdddddddddddddddr Lst)
« Reply #2 on: November 26, 2016, 04:02:29 PM »
There are an abundant number of ways to do it but a simple, nominal code, recursively flavored one might be:

Code: [Select]
(defun cdrn ( n lst )
    (if (and lst (< 0 n))
        (cdrn (1- n) (cdr lst))
        lst
    )
)

(cdrn 7 '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15))

>> (7 8 9 10 11 12 13 14 15)


It's not overly efficient but sometimes uber efficient is not needed. Anyways ... cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: (cdddddddddddddddr Lst)
« Reply #3 on: November 26, 2016, 04:35:24 PM »
Thanks alot guys! :)
(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

ribarm

  • Gator
  • Posts: 3306
  • Marko Ribar, architect
Re: (cdddddddddddddddr Lst)
« Reply #4 on: November 26, 2016, 04:43:38 PM »
Maybe just :

Code: [Select]
(defun cdrn ( n l )
  (repeat n
    (setq l (cdr l))
  )
  l
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: (cdddddddddddddddr Lst)
« Reply #5 on: November 26, 2016, 04:59:06 PM »
Maybe just :

Code: [Select]
(defun cdrn ( n l )
  (repeat n
    (setq l (cdr l))
  )
  l
)
Thats even simplier, reminds me about iterating thru list! :)
By the way should "l" be localised ? (defun cdrn (n l / l) ... ) - no?
(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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (cdddddddddddddddr Lst)
« Reply #6 on: November 26, 2016, 05:02:34 PM »
it's already localised
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (cdddddddddddddddr Lst)
« Reply #7 on: November 26, 2016, 05:05:32 PM »
i.e.

(setq lst '(0 1 2 3 4 5 6 7))

(princ (cdrn 3 lst)) >> (3 4 5 6 7)

(princ lst)          >> (0 1 2 3 4 5 6 7)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: (cdddddddddddddddr Lst)
« Reply #8 on: November 26, 2016, 05:12:39 PM »
Thanks mr.Puckett, I'm still a dummy lisper.
(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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (cdddddddddddddddr Lst)
« Reply #9 on: November 26, 2016, 05:22:43 PM »
You are no dummy and you are most welcome.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: (cdddddddddddddddr Lst)
« Reply #10 on: November 26, 2016, 06:24:52 PM »
Another, for a minor increase in performance for long lists:
Code - Auto/Visual Lisp: [Select]
  1. (defun cdrn ( n l )
  2.     (repeat (/   n 4) (setq l (cddddr l)))
  3.     (repeat (rem n 4) (setq l (cdr    l)))
  4.     l
  5. )

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: (cdddddddddddddddr Lst)
« Reply #11 on: November 26, 2016, 06:53:47 PM »
And another:
Code - Auto/Visual Lisp: [Select]
  1. (defun cdrn ( n l )
  2.     (vl-member-if '(lambda ( x ) (minusp (setq n (1- n)))) l)
  3. )

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: (cdddddddddddddddr Lst)
« Reply #12 on: November 27, 2016, 05:03:04 AM »
And another:
Code: [Select]
(defun cdrn ( n Lst )
(member (nth n Lst) Lst)
)
:wink:
(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

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: (cdddddddddddddddr Lst)
« Reply #13 on: November 27, 2016, 06:25:24 AM »
@Grrr1337:
Your last suggestion is problematic if the list contains duplicate items.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: (cdddddddddddddddr Lst)
« Reply #14 on: November 27, 2016, 06:31:14 AM »
@Grrr1337:
Your last suggestion is problematic if the list contains duplicate items.
Damn, you are correct.
(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