Stoppt die Vorratsdatenspeicherung! Jetzt klicken &handeln! Willst du auch an der Aktion teilnehmen? Hier findest du alle relevanten Infos
  und Materialien:

Skip to content


Lisp macros part 2

I’ve been playing a little more with macros and came up with a foreach-loop, similar to those used in Java or C#.

It basically does the same as the predefined dolist-macro, but oh well :)

foreach macro

1
2
3
4
5
6
7
8
9
10
;; foreach-loop, similar to those in Java or C#
;; example: (foreach (x '(1 2 3))
;;                               (print x))
(defmacro foreach ((var list) &body body)
  `(let ((,var (car ,list)) (rest (cdr ,list)))
    (if ,var
	(progn
	  ,@body
	  (foreach (,var rest) ,@body))
	nil)))

I’ve also come up with my own file-handling macro, similar to the predefined with-open-file:

with-file macro

1
2
3
4
5
;; can be used like with-open-file macro
(defmacro with-file ((filestream-var filename &optional &key (if-exists nil) (direction :input)) &body body)
  `(let ((,filestream-var (open ,filename :direction ,direction :if-exists ,if-exists)))
    ,@body
    (close ,filestream-var)))

At first macros seemed to be something complicated, but once I’ve started reimplementing some of the standard macros and also making up my own custom ones, it really got much clearer to me how they work and why they are so important. Especially the with-file macro gives you an idea of whats possible with macros. Abstracting away the pattern of opening a file, doing something and then closing it again is just really nice, since you won’t ever forget to close it again, if you stick with using the macro.

I’ll try to come up with some more soon. :)

Next big thing on my agenda is some file in- and output as well as CLOS, the powerful Common Lisps Object System.

Posted in General. Tagged with , , , .

4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. foo said

    The for each macro does not work. Try to use it in a function and compile that file. It only works for literal lists in an interpreter mode that does not expand the code completely before executing it. The macro calls itself and dies in recursion.

    You should not use ,list multiple times in the macro.

  2. thanks for the advise.
    i wrote these macros while still using clisp and it seemed to work, but you’re right in that they only work when in interpreted mode.
    i’ve found a better way to do this via tagbody & go… i’ll probably post them here too.

  3. foo said

    Well, they won’t work in a) implementations that use only a compiler (there are several) or use some interpreter variant, where code gets expanded fully before executed.

    You don’t really want to use tagbody in your code. ;-)

  4. i know it’s messy. any other solution how to do it, without using standard looping macros?

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.


Ihr Browser versucht gerade eine Seite aus dem sogenannten Internet auszudrucken. Das Internet ist ein weltweites Netzwerk von Computern, das den Menschen ganz neue Möglichkeiten der Kommunikation bietet.

Da Politiker im Regelfall von neuen Dingen nichts verstehen, halten wir es für notwendig, sie davor zu schützen. Dies ist im beidseitigen Interesse, da unnötige Angstzustände bei Ihnen verhindert werden, ebenso wie es uns vor profilierungs- und machtsüchtigen Politikern schützt.

Sollten Sie der Meinung sein, dass Sie diese Internetseite dennoch sehen sollten, so können Sie jederzeit durch normalen Gebrauch eines Internetbrowsers darauf zugreifen. Dazu sind aber minimale Computerkenntnisse erforderlich. Sollten Sie diese nicht haben, vergessen Sie einfach dieses Internet und lassen uns in Ruhe.

Die Umgehung dieser Ausdrucksperre ist nach §95a UrhG verboten.

Mehr Informationen unter www.politiker-stopp.de.