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

Skip to content


Creating standard control structures in common lisp

I’ve been playing around with (and also learning) common lisp recently and found its macro system very intriguing. So I started to implement some standard imperative control structures that closely resemble those found in most imperative mainstream programming languages like Java or C.

Here’s what I’ve done so far:

while macro:

1
2
3
4
5
6
7
8
;; example:
;; (defparameter x 0)
;; (while (< x 10) (print x) (incf x)) ;; output numbers 0 - 9.
(defmacro while (cond &body body)
  `(if ,cond 
    (progn ,@body 
	   (while ,cond ,@body))
    nil))

until macro

1
2
3
4
5
6
;; example: (until (= x 10) (print x) (incf x))
(defmacro until (cond &body body)
  `(if (not ,cond)
    (progn ,@body
	   (until ,cond ,@body))
    nil))

for macro

1
2
3
4
5
6
7
;; example:
;; (for ((i 0) (< i 10) (incf i))
;;      (print i))
(defmacro for (((var init-value) cond step-block) &body body)
  `(do ((,var ,init-value ,step-block))
    ((not ,cond))
    ,@body))

I started this yesterday and I’ll try to come up with some more. Lets see what else i can come up with ;)
Not that this is particularly useful or anything since you really don’t need them, but it’s still a nice way to learn the language and especially how macros work. I’ll just keep it as a little exercise to myself in order to get to know common lisp in greater detail. :)

Posted in General. Tagged with , , , .

2 Responses

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

  1. Also wenn das mit den Makros so geht (was ich dir natürlich so glaube! ;) ), sind die ja um einiges geiler als in C! sind ja quasi, wie ich das so sehe, eigene komplexe funktionen und mehr…

  2. foo said

    until und while funktionieren auch nicht. Ein Lisp Compiler kann Funktionen, die diese Macros nicht übersetzen -> Endlosrekursion.

    macroexpand while -> macroexpand while -> macroexpand while …

    Als Literatur zu Macros: On Lisp von Paul Graham. Gibt es auch umsonst als PDF.

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.