AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN


Mapcar

Have you ever wondered where you would use the MAPCAR function?
Here is a perfect example :

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 arglist it sets a to 12.0, b to 145.8, c to 67.2 and d to "M20".
 

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