General Question

alley's avatar

What are macros?

Asked by alley (7points) December 9th, 2006
Observing members: 0 Composing members: 0

15 Answers

Supergirl's avatar
A shortcut that will make several things function/occur in a sequence
ben's avatar
Yep. Well stated, amccabe.
Evan's avatar
what are you looking to use macros with?
burlapmellish's avatar
If you are using macros in Word or Excel, trying using the "record new macro" function. Then make your text centered, bold, or other steps, hit stop, and view the macro you just recorded. It will give you a quick look at how they work.
arnieb's avatar
A "Macro" is a single command that causes the execution of a series of commands. Macros can remove some of the drudgery of having to repeat the same sequences of commands over and over again. In programming they are a useful construct which allow a programmer to create "super instructions" that look like a simple instruction, such as MOVE A,B , but actually contain many other instructions. In some languages macros may be nested, where a new macro is defined that includes other macros within in.
divaricatum's avatar
Some programming languages such as Lisp allow you to define operators called 'macros' which provide rules for transforming an expression using that operator into another expression by following the rules provided. The transformation is done by (typically) the compiler or the interpreter. At its simplest, this allows you to perform standard tasks in a shorthand fashion. More powerfully, you can even define your own private programming language which is transformed into the existing already implemented programming language.
divaricatum's avatar
Here is an example in Lisp. When you do input/output to a file, you must open the file, do what you want, and then close the file. If you forget the close the file, eventually the program will fail because you will run out of file descriptors. Lisp has the macro with-open-file which automatically does the opening and closing tasks. Here is Lisp code which does that (I do not expect most people to be able to follow it, but I wanted to show what macro defining code looks like(:
divaricatum's avatar
(defmacro with-open-file ((var &rest; open-args) &body; body)
divaricatum's avatar
`(let ((,var (open ,@open-args)))
divaricatum's avatar
(unwind-protect (progn ,@forms)
divaricatum's avatar
(close ,var))))
divaricatum's avatar
and a call looks like
divaricatum's avatar
(with-open-file (s "myfile" :direction :output)
divaricatum's avatar
(print "Hello!" s))
divaricatum's avatar
and you end up when you run that form with a file named myfile containing Hello!.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther