Feeds:
Posts

## Macro Intro

Been messing around with macros recently and thought I’d give an introduction to the power of macros and the fun you can have with them. Lisp is expressed as lists, which means that you can write programs that write programs. One way to do this is with ‘eval’ : (eval ‘(+ 1 2 3)). This has limitation since a) the expressions are evaluated with no lexical context (defining a variable with a ‘let’ and then calling eval on some expressions, those expressions can’t use that variable) b) it is slow.

That is where macros come into play. Macros are basically a rule for translation. The compiler performs a translation, called macro-expansion. This means the code generated is actually part of your program, thus much faster. An example from Paul Graham’s book “ANSI Common Lisp” is adding a while loop to Lisp:

> (let ((x 10)
(while (< x 10)
(princ x)
(incf x)))
0123456789
NIL

The macro to implement this would be:

(defmacro while (test &rest body)
(do ()
((not ,test))
,@body))

This is a very simple example, but gets the point across. The while macro takes a test argument and then any number of expressions to loop over while this ‘test’ is true. The ‘&rest body’ is a list of the list expressions, in the example above it would contain, ( (princ x) (incf x) ), lists in Lisp are represented with parens, obviously. The backquote  turns evalution off, a comma turns it back on and a comma-at splices the list and evaluates, thus the list in ‘body’ would become two distinct lists and be evaluated one after the other. This allows the macro to be expanded into an expression without being evaluated yet, except when there is a comma which in this case would replace ‘,test’ with ‘(< x 10)’, without the comma ‘test’ would not be replaced and running this code would not work since Lisp would have no idea what ‘test’ is when the macro is expanded (I hope that is understandable, hard to explain it without more examples, just remember that the arguments to defmacro only live in defmacro not where the macro is used).You can do : “(pprint (marcoexpand-1 ‘(…while loop code…)))”, to see what Lisp actually runs after the macro expansion.

I always stupidly tried to use match on a string when I was learning Ocaml. So, I thought as a Lisp learning experience I’d implement it. The code I wanted to be able to use was:

(match str_1
str_2 (princ “first match worked”)
str_3 (princ “second match worked”)
str_4 (princ “third match worked”))

Turned out that this does not need a macro at all, haha. I ended up writing this function:

(defun match (str &rest tests)
(labels ((f_match (args)
(if (equal str (car args))
(apply #'(lambda () (car (cdr args))) ())
(if (null (cddr tests))
()
(f_match (cddr args))))))
(f_match tests)))

This post may have started off as being about macros, but I guess its more about the power of Lisp syntax. I can send a list of Lisp expressions as an argument and then run them. Not sure if (apply ….) is the best way to do it in this situation. It was just the easiest way I knew how to do it. This function is pretty simple. It loops through all the elements of ‘tests’ checking the first element against str and if it matches evaluating the second element. If it does not match it calls f_match recusively with the tail of the list starting at the third element, via the function (cddr tests).

Hope this made sense. I tried to keep it short and probably just made it more confusing by doing so. I’ll have more on macros later, maybe once I come up with a problem that actually needs a macro.

## So Long Gedit, Nedit, Vi and Kile. Hello, XEmacs!

I was always skeptical of XEmacs and Lisp. Lisp is funny looking and so are XEmac’s keybindings. But once you put the two together you may realize that you have been missing out for years, that is how I felt. I started getting into Lisp for the past couple months and thus XEmacs as well. I thought the best thing was C-x C-e. Little did I know it gets a whole hell of a lot better. I hadn’t tried coding things for emacs yet, until I wanted a replacement for my LaTeX editing, was using Kile. Kile needs KDELibs and me no likey that. Found this tutorial on making abbreviations for LaTeX in XEmacs. Now I just needed to make my own for Prosper, the LaTeX presentation addon I use.

In the .xemacs diretory you’ll need init.el so XEmacs knows to load your abbreviations file, check out the link to the tutorial above for this and a few other things you’ll need, such as the function to catch the spacebar and try to match an abbreviation. In another file, abbreviations.el, you can set abbreviations to be matched.

(“\\bs” “” my-prosper-begin-slide 0)

Now aftering typing “\bs” and hitting space the function “my-prosper-begin-slide” will be called.

(defun my-prosper-begin-slide ()
(interactive)
(insert “\\begin{slide}{}\n”)
(insert ” \n”)
(insert “\\end{slide}”)
(previous-line 3)
(forward-char 15)
)

This function replaces “\bs” with the text in insert. After its placed this text I tell it to move up three lines and over 15 characters. Now hitting space after a “\bs” I am right in position to enter the title for that slide!

(defun my-prosper-itemstep ()
(interactive)
(insert “\\begin{itemstep}\n”)
(insert “\\item \n”)
(insert “\\end{itemstep}\n”)
(previous-line 2)
(forward-char 6)
)

Is equally as nice. Just typing “\is” and bam I am enting the first item’s text, after “M-x latex-mode” of course :).

I hope this has interested some people who havn’t yet given emacs a try to put down vi for a minute and find the power of emacs. I know this is pretty basic stuff, and I am sure I’ll be posting more as I delve deeper into the nirvana that is XEmacs.

If anyone wants a copy of my prosper additions just let me know.

## Quantum Answers….no, just more questions actually

Have not done too much reading up on physics, quantum or classic, recently but must post something since Jordan has. You bring up the point of determinism. In my nonexpert opinion the universe is set on a determined path and things such as randomness and “free will” are just illusions caused by our inability to fully grasp quantum physics.

Are we just quantum particles to larger objects? Been debating that one for a long time too. What if we would find out more about our meaning and purpose if we looked “bigger” instead of always trying to look at things in smaller parts?

Passing and storing data? I think in the next couple hundred years the way we store and transmit data will drasticly change. Email will simply be the user creating a set of properties representing that email in another universe that affects the properties of some particles at the receivers computer to mimick the properties of the email you created. Much faster than the speed of light. The creation of another universe isn’t necessarily needed, but you get what I am saying I’m sure. If I actually knew how it would work I wouldn’t be sitting here making XML documents now would I.

Where is God in all this? And where the hell are the gravitons!

## JBoss the Java Saver

JBoss is the one thing stopping me from saying Java is a complete waste. Below I posted not the best part of JBoss, but an interesting part none the less. This code creates a join table between the Project table and StakeHolder table, a many-to-many association. Of course you also have to have your Project Entity Bean (the code below goes in here) and StakeHolder Entity Bean defined, but those are easy, very short too, just some get/sets for the tables attributes (no code involved except for the method names).

/**
* @ejb.interface-method
*
* @ejb.relation name=”projects-to-stakeholders”
* role-name=”Project-has-many-StakeHolders”
* target-ejb=”StakeHolderBean”
* target-role-name=”StakeHolder-has-many-Projects”
* target-multiple=”yes”
*
* @jboss.relation-mapping style=”relation-table”
*
* @jboss.relation-table table-name=”ParentChildrenJoin”
*
* @jboss.relation related-pk-field=”id”
* fk-column=”stakeholderID”
* fk-constraint=”false”
*/
public abstract Collection getStakeHolders();
/** @ejb.interface-method */
public abstract void setStakeHolders(Collection stakeholders);

Now from a Session Bean you can call project.getStakeHolders() and you have all the StakeHolder objects that are related to the project for you to iterate through and mess with its attributes at will, get/set methods will update the tables for you of course. No more making PrepareStatements, looping through ResultSets and the other pains. Using XDoclet (the comments, used for generating code and config files) and JBoss you can generate an advanced application in no time, I created a blog with comments and polls in no time a few years back with limited JBoss experience. It is database independent as well. Write once, deploy anywhere. Maybe I can give a quick presentation on JBoss at one of the meetings.

In more database goodness :

> (select [FieldType] [Researcher] :from [SampleAreas])

Looks like it’ll be fun to use for converting parts of the XQuery implementation that I’m working on currently in Java/SQL to Lisp/SQL, more on that later. Implementing XQuery in Java is what first brought me to the side of hating Java, sorry for not listening Wilber. Check out ODBC or Lisp and SQL.

Hopefully this will get the ball rolling on Chicago GLUG Planet :)