Author Topic: Question of the day #2  (Read 3497 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Question of the day #2
« on: November 23, 2004, 09:20:15 AM »
Given the following list show two ways of extracting the third ("three") item.

Code: [Select]
(setq lst '("one" "two" "three" "four" "five"))
TheSwamp.org  (serving the CAD community since 2003)

whdjr

  • Guest
Question of the day #2
« Reply #1 on: November 23, 2004, 09:30:40 AM »
Here's my two ways.
Code: [Select]
(setq lst '("one" "two" "three" "four" "five"))
(setq 1way (caddr lst))
(setq 2way (nth 2 lst))

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Question of the day #2
« Reply #2 on: November 23, 2004, 11:10:22 AM »
Will, it looks like you got the 2 most common and concise methods.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

whdjr

  • Guest
Question of the day #2
« Reply #3 on: November 23, 2004, 02:15:18 PM »
Yeppeee!!!
What prize do I win?

SPDCad

  • Bull Frog
  • Posts: 453
Question of the day #2
« Reply #4 on: November 23, 2004, 02:38:05 PM »
Is “the question of the day” for newbies?

I would feel funny answering most of these, or even worse I could answer them in such a complex way I might throw off, newbie!

As you mentioned above the two most common ways are shown, but I know of a few other methods, which get complex and basically a waste of time to programme. After all I try to write code is the simplest way possible with the fewest lines of code.
AutoCAD 2010, w/ OpenDCL

visit: http://reachme.at/spd_designs

SMadsen

  • Guest
Question of the day #2
« Reply #5 on: November 23, 2004, 02:59:38 PM »
SPDCad, I think you should feel absolutely free to post your solutions, complex as well as simple.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Question of the day #2
« Reply #6 on: November 23, 2004, 03:01:12 PM »
All I can say is, if it looks like it's for newbies then it probably is. :D

But if you want to show your stuff, be my quest. I'm sure we can all learn something.
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Question of the day #2
« Reply #7 on: November 23, 2004, 03:13:11 PM »
And here is how to return the others.
Code: [Select]

Command: (car lst) "one"
Command: (cadr lst) "two"
Command: (caddr lst) "three"
Command: (cadddr lst) "four"
Command: (last lst) "five"


(setq cntr 0)

  (while (< cntr (1- (length lst)))
         (princ
           (strcat
             "\n"(nth cntr lst)
             )
           )
         (setq cntr (1+ cntr))
         )
TheSwamp.org  (serving the CAD community since 2003)

David Bethel

  • Swamp Rat
  • Posts: 656
Question of the day #2
« Reply #8 on: November 23, 2004, 03:45:42 PM »
Code: [Select]
(caddr lst)

can also be parsed as:

Code: [Select]
(car (cdr (cdr lst)))

-David
R12 Dos - A2K

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Question of the day #2
« Reply #9 on: November 23, 2004, 04:03:08 PM »
How about a redundant method.

Code: [Select]

(car (member (nth 2 lst) lst))
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Question of the day #2
« Reply #10 on: November 23, 2004, 05:05:51 PM »
Quote from: Mark Thomas
And here is how to return the others.

Here are some more.
Code: [Select]
(setq cntr 0)
(repeat (length lst)
  (princ (strcat "\n"(nth cntr lst)))
   (setq cntr (1+ cntr))
)

(foreach x lst
  (princ (strcat "\n" x))
)


(while lst
  (princ (strcat "\n" (car lst)))
  (setq lst (cdr lst))
)
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.

Fuccaro

  • Guest
Question of the day #2
« Reply #11 on: November 24, 2004, 03:23:21 AM »
Code: [Select]
(setq lst '("one" "two" "three" "four" "five"))
(setq way1 (nth 2 lst)
      way2 (nth 2 (reverse lst))
      )

 :D
Or even better:
Code: [Select]
(setq ways (mapcar '(lambda(x) (nth 2 x)) (list lst (reverse lst))))
 :D  :D