Author Topic: mapcar help  (Read 6223 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12923
  • London, England
Re: mapcar help
« Reply #15 on: January 02, 2014, 04:20:26 PM »
Quote
Exercise 1.3.  Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

Without using mapcar:
Code - Auto/Visual Lisp: [Select]
  1. (defun sumsqr ( x y z )
  2.     (cond ((or (<= x y z) (<= x z y))
  3.            (+ (* y y) (* z z)))
  4.           ((or (<= y x z) (<= y z x))
  5.            (+ (* x x) (* z z)))
  6.           ((+ (* x x) (* y y)))
  7.     )
  8. )
Code - Auto/Visual Lisp: [Select]
  1. (defun sumsqr ( x y z / m )
  2.     (setq m (min x y z))
  3.     (cond ((= x m) (+ (* y y) (* z z)))
  4.           ((= y m) (+ (* x x) (* z z)))
  5.           ((+ (* x x) (* y y)))
  6.     )
  7. )

Bhull1985

  • Guest
Re: mapcar help
« Reply #16 on: January 02, 2014, 04:22:32 PM »
Darn, thought I had that one correct.
Well thanks for showing me what I need to study up on you guys!

Oh I really like the second method you used there with min. Perfect example of how experience is key so I went and commented it, committing it to memory best I can

Code: [Select]
(defun sumsqr ( x y z / m)               ;define
(setq m (min x y z))                        ;sets a variable m to be equal to the lowest of the three values
(cond ((= x m) (+ (* y y) (* z z)))   ;checks if x is lowest, thereby qualifying y and z, add the sqrts
         ((= y m) (+ (* x x) (* z z)))   ;checks if y is lowest, thereby qualifying x and z, add the sqrts
         ((+ (* x x) (* y y)))               ;otherwise z must be lowest therefor x and y are qualified, add the sqrts
)                                                    ;cond
)                                                    ;defun
« Last Edit: January 02, 2014, 04:29:59 PM by Bhull1985 »

Lee Mac

  • Seagull
  • Posts: 12923
  • London, England
Re: mapcar help
« Reply #17 on: January 02, 2014, 04:37:45 PM »
Thank you Brandon - judging by your comments, it looks like you understand it well  :-)

Lee Mac

  • Seagull
  • Posts: 12923
  • London, England
Re: mapcar help
« Reply #18 on: January 02, 2014, 04:43:53 PM »
Another for fun:
Code - Auto/Visual Lisp: [Select]
  1. (defun sumsqr ( x y z / l )
  2.     (apply '+ (mapcar '* (setq l (cdr (vl-sort (list x y z) '<))) l))
  3. )

Lee Mac

  • Seagull
  • Posts: 12923
  • London, England
Re: mapcar help
« Reply #19 on: January 02, 2014, 04:45:27 PM »
A recursive variation:
Code - Auto/Visual Lisp: [Select]
  1. (defun sumsqr ( x y z )
  2.     (if (= x (min x y z)) (+ (* y y) (* z z)) (sumsqr y z x))
  3. )

Bhull1985

  • Guest
Re: mapcar help
« Reply #20 on: January 02, 2014, 05:07:29 PM »
Another for fun:
Code - Auto/Visual Lisp: [Select]
  1. (defun sumsqr ( x y z / l )
  2.     (apply '+ (mapcar '* (setq l (cdr (vl-sort (list x y z) '<))) l))
  3. )

Code: [Select]
(defun sumsqr ( x y z / l )              ;define, 3 arguments
(apply                                          ;apply the following
  '+                                              ;add the results together
     (mapcar                                  ;do the following function to each item in the following list
          '*                                       ;telling the mapcar to multiply the values found in the following list by the values of the supplied list
              (setq l (cdr
                           (vl-sort (list x y z) '<)               ;seems like cheating but you used an active-x vl-sort, gave it the list, then told it what to sort by "less than" this places the smallest value in the
                                                                          ;first position in the list, which is why you call (cdr) in the next nested statement, this in effect "finds" the two greatest numbers. Cheater.
                        )                     
               );set a variable 'l' to contain the two larger numbers

           l                                                                ;2nd argument to mapcar, the list to be multiplied BY. These two lists are the same. This squares them

    )          ;the nesting makes it complicated but obviously the (apply '+ (* l l)) has the squares added together.
)

well, obviously I've still got some learning to do but I think i'm getting there. A lot further than I was this morning, that's for sure.
Heading out for the day in a few minutes, enjoy your weekend everyone.
Oh and I was just kidding Lee (about the cheating), your ability to provide alternative methods of accomplishing the same task is as usual, very impressive.
One day I hope to have as great an understanding as you do.

Lee Mac

  • Seagull
  • Posts: 12923
  • London, England
Re: mapcar help
« Reply #21 on: January 02, 2014, 06:11:42 PM »
Code: [Select]
Cheater.

:-D

well, obviously I've still got some learning to do but I think i'm getting there. A lot further than I was this morning, that's for sure.
Heading out for the day in a few minutes, enjoy your weekend everyone.

Oh and I was just kidding Lee (about the cheating), your ability to provide alternative methods of accomplishing the same task is as usual, very impressive.

One day I hope to have as great an understanding as you do.

Don't worry - I saw the funny side of your comments  :-)
For the record, I also think its cheating  :lol:

Thank you for the kind compliments - I'm glad your learning is progressing and I'm happy to offer some food for thought as I very much enjoy writing the examples and finding new ways to tackle problems.

Have a good weekend Brandon  :-)