(mapcar) and (lambda) _ AfraLISP
(mapcar) and (lambda) _ AfraLISP
Site Map
Search
AutoLISP
Beginners' Tutorials
Intermediate Tutorials
Advanced Tutorials
Application Tutorials
Tips'n'Tricks
Visual LISP
Beginners' Tutorials
Intermediate Tutorials
DCL
Beginners' Tutorials
Intermediate Tutorials
VBA
VBA Tutorials
VBA Applications
Tips'nTricks
AutoCAD
Customization Tutorials
Tips'n'Tricks
Reference
AutoLISP Functions
Visual LISP Sample Files
DXF Group Codes
The AutoCAD APIs
DCL Tile Attributes
AutoLISP DCL Functions
System Variables
AutoCAD Object Model
Sin and Cos Functions
VLAX Enumeration
Download
Forum
As you know, LISP stands for "List Processing". There are quite a few commands in AutoLisp that allow you to manipulate lists. (subst, append, etc.) But what about
commands that will allow you to apply a function to items in a list. Let's look at (mapcar) first.
(mapcar)
The function (mapcar) allows you to perform a "function" on each element of the list. Let's try a simple example :
(setq a 1)
(mapcar '1+ (list a))
(2 3 4 5 6 7 8 9 10 11)
Just a few words on creating lists in AutoLisp: there are two ways to create lists. The first way is to use a command that will create a list. Examples of this are
(getpoint) and (entget). Both of these commands will return a list.
Secondly, you could use the (list) command. For example :
(1 2 3 4 5)
You want to place each item in the list in it's own variable to use in your routine. One way to do it would be as follows :
This works, but is an extremely slow way of processing the data as each variable requires a program statement. A much more efficient way is to use the MAPCAR
technique.
This routine maps the SET function to each element of the first list and it's corresponding element of the second list. SET is used instead of SETQ to evaluate each
quoted element of the first list. With the currently set list c, it sets a to 12.0, b to 145.8, c to 67.2 and d to "M20".
If you are reading a list from an external file, your routine may not read back the elements of the list as they once were. Your routine will read them back as strings. For
example :
(10 20 30 40 50)
But after reading the list in from the file, it looks like this :
You can use (mapcar) to convert the list from strings to integers :
Now this works fine if you are using an AutoLisp function, but how would you use (mapcar) with a user defined function? Let's look at this example :
This function will run the (dtr) function against each element of list c. In other words, the value of each element is passed to the (dtr) function.
(lambda)
Now this is where the (lambda) function comes into play. (lambda) allows you to write the (dtr) function "in-line" within the (mapcar) expression without having to
define a separate function.
or
This function will convert all the angles in the list c to radians and store them in variable d.
(defun c:test ()
(setq c '(23.0 47.8 52.1 35.6))
(setq d (mapcar '(lambda (a) (* pi (/ a 180.0))) c))
(mapcar 'set '(w x y z) d)
(princ)
)
"Use the (lambda) function when the overhead of defining a new function is not justified. It also makes the programmer's intention more apparent by laying out the
function at the spot where it is to be used."
In practice, (lambda) can be used anywhere you need a speedy function and you don't want the trouble of writing and making sure a (defun) function is loaded.
There are another two AutoLisp commands that can apply a function to a list. They are (apply) and (foreach). Let's have a quick look at them.
(apply)
This function differs from (mapcar) in that it applies a function to the whole list and not to the individual items in the list. Here's a couple of examples :
(apply '(lambda (x y z)
(* x (- y z))
)
'(5 20 14)
)
(foreach)
The syntax for this is: (foreach name list expression…)
This function steps through list, assigning each element to name, and evaluates each expression for every element in the list. For example :
(setq a (1 2 3 4 5))
1
2
3
4
5
Right, I don't know about you, but my brain is full. Time for a couple of beers. Cheers for Now…
AutoLISP
AutoLISP Beginners' Tutorials
AutoLISP Intermediate Tutorials
AutoLISP Advanced Tutorials
AutoLISP Application Tutorials
AutoLISP Tips'n'Tricks
AfraLISP Archive
‘Hey, what's happened to AfraLISP?’ If you've visited our site before, you'll notice some big changes. We're currently revamping the entire site to bring you updated
tutorials and a better user experience. However, if there's something you can't find, the AfraLISP Archive contains a full copy of the original site as originally created by
Kenny Ramage.
Online Books
The ABC's of AutoLISP
The Visual LISP Developer's Bible
AutoLISP Forums
CADTutor
Autodesk Discussion Groups
Autodesk User Group International (AUGI)
The Swamp
Back to top
Home
Cared for by David Watson © 2024