Author Topic: mapcar list question  (Read 5856 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Bull Frog
  • Posts: 483
mapcar list question
« on: March 02, 2011, 01:25:08 PM »
I can't figure out the secret sauce to the following -

Giving 2 lists
L1 = (1.0 2.0 3.0)
L2 = ((1.0 2.0 3.0) (4.0 5.0 6.0) (7.0 8.0 9.0))

How can L1 be added to each sublist in L2?

New tread (not retired) - public repo at https://github.com/pkohut

T.Willey

  • Needs a day job
  • Posts: 5251
Re: mapcar list question
« Reply #1 on: March 02, 2011, 01:28:26 PM »
(mapcar (function (lambda ( x ) (mapcar (function +) x L1))) L2)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

pkohut

  • Bull Frog
  • Posts: 483
Re: mapcar list question
« Reply #2 on: March 02, 2011, 01:50:49 PM »
Thanks Tim. Now how to flatten the returned result
((2.0 4.0 6.0) (5.0 7.0 9.0) (8.0 10.0 12.0))
to
(2.0 4.0 6.0 5.0 7.0 9.0 8.0 10.0 12.0)
as I need to (apply '+ flattenedList)

ps, these are simplified examples for illustration purposes only. The actual 'functions are more complex.

TIA
New tread (not retired) - public repo at https://github.com/pkohut

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: mapcar list question
« Reply #3 on: March 02, 2011, 01:53:55 PM »
Code: [Select]
(apply 'append lst)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

pkohut

  • Bull Frog
  • Posts: 483
Re: mapcar list question
« Reply #4 on: March 02, 2011, 01:55:37 PM »
Code: [Select]
(apply 'append lst)

Wonderful.  :-)
New tread (not retired) - public repo at https://github.com/pkohut

pkohut

  • Bull Frog
  • Posts: 483
Re: mapcar list question
« Reply #5 on: March 02, 2011, 05:08:15 PM »
Ok, here's where I'm at. Currently looking at some code that is written for Clojure Lisp, which I know zip about.

I can't figure out how to convert the following line of Clojure code to autolisp.
Code: [Select]
(apply + (map #(VectorSubtract L1 %) L2))Again, it is simplified for illustration purposes. VectorSubtract in AL would be
Code: [Select]
(defun VectorSubtract (v u / )
    (mapcar '- v u)
)

Running the code by hand the final result should be (I think)
Code: [Select]
(9.0 9.0 9.0)
Code: [Select]
L1 = (1.0 2.0 3.0)
L2 = ((1.0 2.0 3.0) (4.0 5.0 6.0) (7.0 8.0 9.0))

VectorSubtract (1.0 2.0 3.0) (1.0 2.0 3.0) => (0.0 0.0 0.0)
VectorSubtract (1.0 2.0 3.0) (4.0 5.0 6.0) => (3.0 3.0 3.0)
VectorSubtract (1.0 2.0 3.0) (7.0 8.0 9.0) => (6.0 6.0 6.0)
                               Final Result = (9.0 9.0 9.0)

Not knowing the fine art of mapcar, lambda, and other such lispy things, this is just out of my reach.

Again TIA for any help
New tread (not retired) - public repo at https://github.com/pkohut

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: mapcar list question
« Reply #6 on: March 02, 2011, 05:11:37 PM »
Give this a try:

Code: [Select]
(apply 'mapcar (cons '+ (mapcar '(lambda ( e ) (mapcar '- e l1)) l2)))

pkohut

  • Bull Frog
  • Posts: 483
Re: mapcar list question
« Reply #7 on: March 02, 2011, 05:15:29 PM »
Give this a try:

Code: [Select]
(apply 'mapcar (cons '+ (mapcar '(lambda ( e ) (mapcar '- e l1)) l2)))

Amazing.
New tread (not retired) - public repo at https://github.com/pkohut

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: mapcar list question
« Reply #8 on: March 02, 2011, 05:36:17 PM »

or if you really wanted to use the VectorSubtract ...

Code: [Select]
(apply 'mapcar (cons '+ (mapcar '(lambda ( v ) (VectorSubtract v L1)) L2)))
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pkohut

  • Bull Frog
  • Posts: 483
Re: mapcar list question
« Reply #9 on: March 02, 2011, 09:00:20 PM »
Thanks Kerry, though it didn't produce the expected result it was a good effort. The others produce the correct results with the  quoted function versions, not with my user functions. After lots of futzing around I managed this version of total-gravitaition-force to work. The Clojure Lisp version is there too.
Code: [Select]
(defun total-gravitational-force (state / tmp pt1 pt2)
  (setq tmp nil
pt1 (cadr (assoc 'player state))
pt2 (cadr (assoc 'obstacles state)))
   (apply 'mapcar (cons '+ (mapcar '(lambda (x) '+ pt1 x) (foreach x pt2 (setq tmp (append tmp (list (gravity-vector pt1 x))))))))
  )
;;;;;; Clojure lisp version of the above ;;;;;;;
(apply + (map #(gravity-vector (:player @state) %) (:obstacles @state))))

Here's the gravity vector function, though it's not implemented right. Need to check out Wikipedia. (the VectorSubtraction test plugin works)
Code: [Select]
(defun gravity-vector (v u / force uvAngle |u| |v|)
  (setq force (/ 2000.0 (expt (magnitude (mapcar '- u v))) 2.0)
|u| (unit u)
|v| (unit v)
uvAngle (- (atan (cadr |v|) (car |v|)) (atan (cadr |u|) (car |u|))))
  ;(list (* (sin uvAngle) force) (* (cos uvAngle) force) 0.0)
  (list (* (sin uvAngle) force) (* (cos uvAngle) force) 0.0)
  )

And here is the state date that is fed to the functions.
Code: [Select]
((PLAYER (237.796 315.332 0.0))
(TARGET (237.796 311.469 0.0))
(OBSTACLES (
    (135.0 223.0 0.0) (560.0 290.0 0.0) (550.0 120.0 0.0)
    (145.0 70.0 0.0) (350.0 320.0 0.0) (60.0 50.0 0.0)
    (210.0 313.0 0.0) (450.0 210.0 0.0))))


Code: [Select]
(total-gravitational-force state) ;;; returns a UVW vector
New tread (not retired) - public repo at https://github.com/pkohut

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: mapcar list question
« Reply #10 on: March 02, 2011, 09:38:23 PM »
Thanks Kerry, though it didn't produce the expected result it was a good effort. < ... >

Well that's just weird, 'cause it works for me  :)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pkohut

  • Bull Frog
  • Posts: 483
Re: mapcar list question
« Reply #11 on: March 02, 2011, 10:16:09 PM »
Thanks Kerry, though it didn't produce the expected result it was a good effort. < ... >

Well that's just weird, 'cause it works for me  :)

Yep, working now  :-)  Must have changed something while reforming the statement.

BTW, why does the first mapcar not have a open paren? Is there some rule for when to use an open paren with the quote? Basically why not this
'(mapcar ...
Code: [Select]
(apply [color=red]'mapcar[/color] (cons '+ (mapcar '(lambda ( v ) (VS v l1)) l2)))
New tread (not retired) - public repo at https://github.com/pkohut

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: mapcar list question
« Reply #12 on: March 02, 2011, 11:37:39 PM »
The quote symbol acts on the following statement.
By using '(xxxxx you are quoting the entire statement included in the braces ...
which produces a completely different result
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: mapcar list question
« Reply #13 on: March 03, 2011, 02:44:02 PM »
BTW, why does the first mapcar not have a open paren? Is there some rule for when to use an open paren with the quote? Basically why not this
'(mapcar ...
Code: [Select]
(apply [color=red]'mapcar[/color] (cons '+ (mapcar '(lambda ( v ) (VS v l1)) l2)))


The highlighted mapcar is an argument (hence not to be evaluated) to the apply function, and so it is quoted in the same way that the lambda function or '+' function is quoted.

We are effectively doing this (using my original code as an example):


Code: [Select]
(apply 'mapcar (cons '+ (mapcar '(lambda ( e ) (mapcar '- e l1)) l2)))
Setting some values:

Code: [Select]
_$ (setq l2 '((1.0 2.0 3.0) (4.0 5.0 6.0) (7.0 8.0 9.0)))
((1.0 2.0 3.0) (4.0 5.0 6.0) (7.0 8.0 9.0))
_$ (setq l1 '(1.0 2.0 3.0))
(1.0 2.0 3.0)

Breaking it down:

Code: [Select]
_$ (mapcar '(lambda ( e ) (mapcar '- e l1)) l2)
((0.0 0.0 0.0) (3.0 3.0 3.0) (6.0 6.0 6.0))

Code: [Select]
_$ (cons '+ (mapcar '(lambda ( e ) (mapcar '- e l1)) l2))
(+ (0.0 0.0 0.0) (3.0 3.0 3.0) (6.0 6.0 6.0))

Code: [Select]
_$ (apply 'mapcar (cons '+ (mapcar '(lambda ( e ) (mapcar '- e l1)) l2)))
(9.0 9.0 9.0)

But notice that:

Code: [Select]
(apply 'mapcar '(+ (0.0 0.0 0.0) (3.0 3.0 3.0) (6.0 6.0 6.0)))
is the same as:

Code: [Select]
(mapcar '+ '(0.0 0.0 0.0) '(3.0 3.0 3.0) '(6.0 6.0 6.0))

Hope this helps with the understanding.




pkohut

  • Bull Frog
  • Posts: 483
Re: mapcar list question
« Reply #14 on: March 03, 2011, 02:52:16 PM »
But notice that:

Code: [Select]
(apply 'mapcar '(+ (0.0 0.0 0.0) (3.0 3.0 3.0) (6.0 6.0 6.0)))
is the same as:

Code: [Select]
(mapcar '+ '(0.0 0.0 0.0) '(3.0 3.0 3.0) '(6.0 6.0 6.0))


Thanks for the clear explanation, especially that last bit. Yesterday I couldn't grasp what the code was doing.
New tread (not retired) - public repo at https://github.com/pkohut