TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: David Bethel on October 20, 2015, 05:11:50 AM
-
Can anyone think of a combination of car/cdr that could extract the X & Y values of a point group
(setq p '(10 2 4 6))
-> (2 4)
Instead of the kludge :
(setq p2 (list (nth 1 p) (nth 2 p))
Thanks -David
-
(reverse (cdr (reverse (cdr p))))
Not sure its any less of a kludge.
Others:
(list (cadr p) (caddr p))
(mapcar 'nth '(1 2) (list p p))
(mapcar 'progn '(0 0) (cdr p))
-
LOL
I looked at the double reverse - way ugly 8-)
-
I don't understand :
(mapcar 'progn '(0 0) (cdr p))
I thought it should be :
(mapcar '+ '(0 0) (cdr p))
-
LOL
I looked at the double reverse - way ugly 8-)
:-D
-
I don't understand :
(mapcar 'progn '(0 0) (cdr p))
I thought it should be :
(mapcar '+ '(0 0) (cdr p))
Why?
I think it should be:
(mapcar 'progn '(Lee Mac) (cdr p))
:lol:
-
David,
FWIW,
(benchmark '((butlast (cdr p)) (list (nth 1 p) (nth 2 p)) (list (cadr p) (caddr p))))
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):
(BUTLAST (CDR P))..............1591 / 1.19 <fastest>
(LIST (CADR P) (CADDR P))......1794 / 1.05
(LIST (NTH 1 P) (NTH 2 P)).....1888 / 1.00 <slowest>
ymg
-
I'm a lisp neophyte but I prefer clarity over conciseness/speed for maintenance and comprehension reasons and I would do it something like this if I knew the point group list was always the same.
(note: this is in scheme but you get the picture)
(define (x-point pgroup)
(cadr p))
(define (y-point pgroup)
(caddr p))
output:
> (x-coord '(10 2 4 6))
2
> (y-coord '(10 2 4 6))
4
If more sample code was written like this (i.e. with simple helper functions) I think the logic behind the function would be easier to understand.
Don't mind me though, just having a play and learning from you guys :)