Skip to content

Instantly share code, notes, and snippets.

Reader Macros in Common Lisp

Reader macros are perhaps not as famous as ordinary macros. While macros are a great way to create your own DSL, reader macros provide even greater flexibility by allowing you to create entirely new syntax on top of Lisp.

Paul Graham explains them very well in [On Lisp][] (Chapter 17, Read-Macros):

The three big moments in a Lisp expression's life are read-time, compile-time, and runtime. Functions are in control at runtime. Macros give us a chance to perform transformations on programs at compile-time. ...read-macros... do their work at read-time.

Macros and read-macros see your program at different stages. Macros get hold of the program when it has already been parsed into Lisp objects by the reader, and read-macros operate on a program while it is still text. However, by invoking read on this text, a read-macro can, if it chooses, get parsed Lisp objects as well. Thus read-macros are at least as powerful as ordinary macros.

@pqnelson
pqnelson / numerical.js
Created September 29, 2014 16:36
Rudimentary numerical analysis tools, written for node.js
var exports = module.exports = {};
/**********************************************************
* @author Alex Nelson
* @email [email protected]
* @date 29 September 2014
**********************************************************/
/**
* My todo list...
@pqnelson
pqnelson / number-thy.cpp
Created August 14, 2014 16:54
Pell numbers, Fibonacci numbers, memoized, and GCD...oh my...
#include <functional>
#include <iostream>
#include <string>
#include <map>
/*
* An example of some number theory calculations, done with dynamic
* programming.
*
* Partially inspired by http://programminggenin.blogspot.com/2013/01/memoization-in-c.html
// $ g++ -std=c++11 object.cpp
// $ ./a.out
// Object@0x00d01010
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip> // for setw, setfill
#include "types.hpp"
#include "object.hpp"
@pqnelson
pqnelson / .emacs
Last active January 16, 2017 23:37
Emacs config file
;; set 72 character width
(setq-default fill-column 72)
(setq auto-fill-mode t)
(setq-default auto-fill-function 'do-auto-fill)
(global-font-lock-mode 1)
(add-hook 'shell-mode-hook
'(lambda () (auto-fill-mode nil)))
;; font face
@pqnelson
pqnelson / math_helper.clj
Last active August 29, 2015 13:58
Math helps
(ns math-helper
:require [clojure.set :as set-utils])
(defn float? [x] (instance? java.lang.Float x))
(defn double? [x] (instance? java.lang.Double x))
(defmacro defconst [const-name const-val]
`(def
~(with-meta const-name
(assoc (meta const-name) :const true))
@pqnelson
pqnelson / euler-population.clj
Created April 6, 2014 19:46
A Monte Carlo analysis of a problem by Euler.
(ns euler-population.core)
;; "After the Flood, all people are descended from six people. If we
;; suppose that after 200 years the population had grown to 1,000,000,
;; then by what part must the population grow each year?"
;; — Leonhard Euler's "Introductio in Analysin Infinitorum", Ch. 6, example 3
(def initial-conditions {:men 3
:women 3})
@pqnelson
pqnelson / toy-obj.clj
Last active August 29, 2015 13:57
This is the relevant code snippets describing a toy object system in Clojure, taken from Amit Rathore's "Clojure in Action"
;; from Amit Rathore's "Clojure in Action", source code taken from his site, freely and legally avaible
;; inheritence
(declare this find-method)
(defn new-object [klass]
(let [state (ref {})]
(fn thiz [command & args]
(condp = command
:class klass