AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

(mapcar) and (lambda).

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 :

What we want to do is add 1 to each element of a list.

	(setq a 1)
	(mapcar '1+ (list a))

This will return 2.
This is what happened :

(mapcar...adds 1 to the (list a) to make 2.

Now a longer list :

	(setq a '(1 2 3 4 5 6 7 8 9 10))
	(mapcar '1+ a)

This will return :

	(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 :

	(setq a (list 1 2 3 4 5))

If you looked at variable a it would look like this :

	(1 2 3 4 5)

The other way of writing this is :

	(setq a '(1 2 3 4 5))

Both methods do the same thing, create a list. 


Here is another example using (mapcar) :

Say you have a list of data stored in variable arglist

(setq arglist '(12.0 145.8 67.2 "M20"))

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 :

   (setq a (nth 0 arglist))
   (setq b (nth 1 arglist))
   (setq c (nth 2 arglist))
   (setq d (nth 3 arglist))

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.

(mapcar 'set '(a b c d) arglist)

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 :

Your list should look like this :

	(10 20 30 40 50)

But after reading the list in from the file, it looks like this :

	("10" "20" "30" "40" "50")

You can use (mapcar) to convert the list from strings to integers :

	(setq b (mapcar '(atoi) thelist)) 

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 :

What we have is a list of angles that we want to convert to Radians.

	(setq c '(23.0 47.8 52.1 35.6))

Firstly we would write a function to convert degrees to radians.

(defun dtr (a)
	(* pi (/ a 180.0))
)

Our function to convert our list would look like this :

	(setq d (mapcar 'dtr c))

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.

The function could also be written like this :

	(setq d (mapcar (quote dtr) c)) 

(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.

	(setq d (mapcar (quote (lambda (a) (* pi (/ a 180.0)))) c))

or

	(setq d (mapcar '(lambda (a) (* pi (/ a 180.0))) c))

This function will convert all the angles in the list c to radians and store them in variable d.


Let's look a bit closer at the (lambda) function.
	(lambda (a) (* pi (/ a 180.0)))

is the same as

	(defun (a) (* pi (/ a 180.0)))

Let's write a function to test this :

(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)
)

!c should return (23.0 47.8 52.1 35.6)
!d should return (0.401426 0.834267 0.909317 0.621337)
!w should return 0.401426
!x should return 0.834267
!y should return 0.909317
!z should return 0.621337 


To quote the AutoCad Customization Manual :

"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 '+ '(1 2 3))

This will return 6

	(apply 'strcat '("a" "b" "c"))

This will return "abc"

You can also use (apply) in comjunction with (lambda) :

	(apply '(lambda (x y z)
		(* x (- y z))
		)
		'(5 20 14)
	)

This will return 30 (20-14*5=30)


(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 :

	(foreach n a (princ n) (terpri))

If you had a list :

	(setq a (1 2 3 4 5))

The previous expression would print vertically :

	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....
 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website