The less-familiar parts of Lisp for beginners — prog and prog*

Next in our survey of possibly unfamiliar Lisp features is a pair of somewhat unusual macros, prog and prog*.  These macros fall squarely into the category of “syntactic sugar”.  There’s nothing particularly special about these, they simplify the establishment of a particular sort of mini-environment.

Within prog, the programmer can declare, and optionally initialize, variables.  Then, after an optional declaration (see declare), appears a sequence of go tags and statements.  A go tag is any bare symbol or integer, not in parentheses, and is not evaluated.  A statement is enclosed in parentheses, and is evaluated when flow encounters it.  Within the statements, the programmer will typically use go to branch to a go tag in the prog body.  Execution of the prog ends when flow passes to the end of the list of statements, or if a return statement is encountered (the entire body is enclosed in an unnamed block).

The typical use case for this is a state machine.  Variables are assigned, flow enters the body, and operations are performed on data, with branches to different tags based on the values encountered.

The difference between prog and prog* is exactly like that between do and do*, or let and let*, in the variable declaration region.  The * form of the macro allows variables declared in the list to depend on earlier variables in the same declaration.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.