Author Topic: mapcar - remove z from points - lambda?  (Read 2044 times)

0 Members and 1 Guest are viewing this topic.

curmudgeon

  • Newt
  • Posts: 192
mapcar - remove z from points - lambda?
« on: July 08, 2009, 09:30:59 AM »
I want to make a routine to use 3 point input to create arcs and/or lwpolys. I have

Code: [Select]
(setq c (getpoint (setq b (getpoint (setq a (getpoint "\n Pick beginning point: ")) "\n Pick point on arc :" )
   ) "\n Pick ar endpoint: " ))

to get my points, and

(list a b c)   
gives me
((-300.904 670.919 0.0) (-325.601 846.356 0.0) (-473.783 879.25 0.0))
as an example.

I am wanting to strip out the z component, and I think I am close with
Code: [Select]
(mapcar  '(lambda (x) (list (car x)(cadar x))  (list a b c)))
but I get errors.

help?
Never express yourself more clearly than you are able to think.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
Re: mapcar - remove z from points - lambda?
« Reply #1 on: July 08, 2009, 09:35:03 AM »
Code: [Select]
(mapcar  '(lambda (x) (list (car x)(cadr x)))  (list a b c))
Stay home. Stay safe. Save lives.

curmudgeon

  • Newt
  • Posts: 192
Re: mapcar - remove z from points - lambda?
« Reply #2 on: July 08, 2009, 09:38:51 AM »
YES !

many thanks.

roy
Never express yourself more clearly than you are able to think.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
Re: mapcar - remove z from points - lambda?
« Reply #3 on: July 08, 2009, 09:40:46 AM »
variant 2
Code: [Select]
(mapcar 'list (mapcar 'car l) (mapcar 'cadr l))
Stay home. Stay safe. Save lives.

gile

  • Water Moccasin
  • Posts: 2258
  • Marseille, France
Re: mapcar - remove z from points - lambda?
« Reply #4 on: July 08, 2009, 09:44:23 AM »
Hi,

(mapcar  '(lambda (x) (list (car x)(cadar x))  (list a b c)))

First they're 2 mistakes in this expression cadar instead of cadr and a misplaced parent.
Should have been:
(mapcar  '(lambda (x) (list (car x) (cadr x)))  (list a b c))
But this expression will return a 2d points list but never change a, b, c values

If you want to change the variables values you can write:
with mapcar and 2lists
Code: [Select]
(mapcar
  '(lambda (v x) (set v (list (car x) (cadr x))))
  '(a b c)
  (list a b c)
)

with mapcar and a single list
Code: [Select]
(mapcar
  '(lambda (x / val)
     (setq val (eval x))
     (set x (list (car val) (cadr val)))
   )
  '(a b c)
)

with foreach
Code: [Select]
(foreach x '(a b c)
  ((lambda (val)
     (set x (list (car val) (cadr val)))
   )
    (eval x)
  )
)
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
Re: mapcar - remove z from points - lambda?
« Reply #5 on: July 08, 2009, 09:47:49 AM »
variant 3
Code: [Select]
(mapcar '(lambda (x) (mapcar '+ x '(0 0))) l)
variant 4
Code: [Select]
(mapcar '(lambda (x) (reverse (cdr (reverse x)))) l)
Stay home. Stay safe. Save lives.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
Re: mapcar - remove z from points - lambda?
« Reply #6 on: July 08, 2009, 09:51:13 AM »
variant 5
Code: [Select]
(apply 'mapcar (cons 'list (reverse (cdr (reverse (apply 'mapcar (cons 'list l)))))))
Stay home. Stay safe. Save lives.

curmudgeon

  • Newt
  • Posts: 192
Re: mapcar - remove z from points - lambda?
« Reply #7 on: July 08, 2009, 10:03:49 AM »
when you say things like
Quote
First they're 2 mistakes in this expression ..

you have my full attention.
but you had that anyway.

the cadar was, I believe, a typographical error introduced while I was trying different ways to get new errors.
 :-D

again, thanks.
I will play with each of the variants to try to comprehend ( get my head around ) the why of it all.
Never express yourself more clearly than you are able to think.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
Re: mapcar - remove z from points - lambda?
« Reply #8 on: July 08, 2009, 10:06:40 AM »

I will play with each of the variants to try to comprehend ( get my head around ) the why of it all.

variant recursion...
Code: [Select]
(defun fun (l)
 (if l
  (cons (list (caar l) (cadar l)) (fun (cdr l)))
 ) ;_  if
) ;_  defun

(fun l) =>> ((-300.904 670.919) (-325.601 846.356) (-473.783 879.25))
Stay home. Stay safe. Save lives.