AutoLISP Again: mapcar

Image: AutoCAD icon.

'mapcar' is the next thing I need to understand

This codeblock (below) is a complete AutoLisp program. Load it into a running instance of AutoCAD and issue the command DOAMAPCAR. Nothing will happen on the screen, but you will see quite a bit of output into the console.

The comments in the code combined with the output in the console window tell you lots of stuff.

The Code

;; WORKING_mapcar.lsp
;; What is mapcar, and what is it good for?
;; Gregory A Sanders, Fall, 2022
;; 
;; This exercise uses:
;;      defun
;;      setq
;;      mapcar
;;      append
;;      print
;;      "\n" - (new line)
;;      princ
;;      strcat
;;      itoa
;;
;; mapcar generates a list.  
;; mapcar requires a minimum of two arguments (options).
;; The first argument is the name of the function that will crunch one or more lists.
;; The second argument is the list that the function will crunch.
;; If there are more lists that the function should crunch, they are added as subsequent arguments.
;; The number of arguments given to 'mapcar' must equal the number of arguments the function requires.
;; The arguments must also be the right type.  Don't send a list of lists to a function that expects a single value.
;; 'mapcar' just makes a list of the function's output, it does not operate on the arguments itself.
;; 
;; So the command looks something like this:
;;
;; (mapcar function list1 list2 list3)
;;
(defun c:doamapcar ( / list1 list2 list3)         ; Our main function definition with lists as local variables.
                                                  ; Define the internal function that our mapcar will run
  (defun add7ThenMultBy2 (invar)                  ; 'invar' is a supplied value. 'mapcar' will get it from a list.
    (setq invar (+ 7 invar))                      ; Add 7 to the supplied value.
                                                  ; Show invar on console on a new line.
                                                  ; Use 'strcat' to concatenate space delimited strings into 
                                                  ; one string.  print and princ can only handle one string.
    (princ (strcat "\n" (itoa (- invar 7)) " + 7 = " (itoa invar)))
                                                  ; 'itoa' turns an integer into a string (alpha).
    (setq invar (* 2 invar))                      ; Multiply invar by 2.
    (print invar)                                 ; Show this new invar value on a new line.
  )                                               ; Close (defun add7ThenMultBy2 .
  (princ "\n:: This .lsp demonstrates 'mapcar'.")
  (setq list1 (list 10 12 13 14 15))              ; Create list1.
  (princ "\nFirst we created list1:")(princ list1)  ; On a new line, put the specified string. Then the list.
  (setq list1 (mapcar '1+ list1))                 ; Add 1 to each value in list1.
  (princ "\nNow add 1 to each number using 'mapcar'.")
  (princ list1)                                   ; Show list1 on the console without a new line.
  (princ "\nCreate list2 using mapcar.\n'mapcar' is like a 'foreach' on list1.")
  (princ "\n'mapcar' runs the 'add7ThenMultby2' function on each value in list1.")
  (princ "\nAllow me to demonstrate:")
  (setq list2 (mapcar 'add7ThenMultBy2 list1))    ; Run each value in list1 through 
                                                  ; the add7ThenMultBy2 function and save it to List2.
  (princ "\nHere is our list2: ")(princ list2)    ; Show list2 on the console.
  (princ "\nUse 'append' to add the contents of list2 to list1.")
  (setq list1 (append list1 list2))               ; 'append' puts the contents of list2 into list1 at the end.
  (princ "\nThe new list1: ")(princ list1)        ; Show us the new list.
  (princ "\nUse 'mapcar' again like before.")
  (setq list3 (mapcar 'add7ThenMultBy2 list1))    ; Run each value in list1 and list2 through 
                                                  ; the add7ThenMultBy2 function and save it to List3.
  (princ "\nHere is our list3: ")(princ list3)    ; Show list3 on the console.
  (princ)                                         ; End clean on the console by showing nothing.
)                                                 ; Close (defun c:doamapcar .

You will get this printout on the console:

Image: AutoCad's console printout.
Image: If you code it right, the console can help you learn this stuff.

Well, that's it! 'mapcar' has been manipulated and the lightbulb has lit. A Thing That Works!

link to home page

links

-->