Author Topic: 2D Point car/cdr  (Read 2158 times)

0 Members and 1 Guest are viewing this topic.

David Bethel

  • Swamp Rat
  • Posts: 656
2D Point car/cdr
« 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

Code: [Select]
(setq p '(10 2 4 6))

-> (2 4)



Instead of the kludge :

Code: [Select]
(setq p2 (list (nth 1 p) (nth 2 p))

Thanks  -David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: 2D Point car/cdr
« Reply #1 on: October 20, 2015, 05:27:24 AM »
Code: [Select]
(reverse (cdr (reverse (cdr p))))
Not sure its any less of a kludge.

Others:
Code: [Select]
(list (cadr p) (caddr p))
Code: [Select]
(mapcar 'nth '(1 2) (list p p))
Code: [Select]
(mapcar 'progn '(0 0) (cdr p))
« Last Edit: October 20, 2015, 05:31:35 AM by Lee Mac »

David Bethel

  • Swamp Rat
  • Posts: 656
Re: 2D Point car/cdr
« Reply #2 on: October 20, 2015, 05:32:27 AM »
LOL

I looked at the double reverse - way ugly  8-)
R12 Dos - A2K

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: 2D Point car/cdr
« Reply #3 on: October 20, 2015, 05:52:27 AM »
I don't understand :

Code: [Select]
(mapcar 'progn '(0 0) (cdr p))

I thought it should be :

Code: [Select]
(mapcar '+ '(0 0) (cdr p))
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: 2D Point car/cdr
« Reply #4 on: October 20, 2015, 06:04:15 AM »
LOL

I looked at the double reverse - way ugly  8-)

 :-D

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: 2D Point car/cdr
« Reply #5 on: October 20, 2015, 06:53:04 AM »
I don't understand :

Code: [Select]
(mapcar 'progn '(0 0) (cdr p))

I thought it should be :

Code: [Select]
(mapcar '+ '(0 0) (cdr p))

Why?

I think it should be:
Code: [Select]
(mapcar 'progn '(Lee Mac) (cdr p))

 :lol:

ymg

  • Guest
Re: 2D Point car/cdr
« Reply #6 on: October 21, 2015, 01:34:23 PM »
David,

FWIW,

Quote
(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


MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: 2D Point car/cdr
« Reply #7 on: October 21, 2015, 04:35:01 PM »
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 :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien