Git Product home page Git Product logo

on-lisp-macros's Introduction

macros

This is some experimenting with Common Lisp following the excellent “On Lisp” by Paul Graham:

First let’s experiment with the Common Lisp macro member

(member "2" '("1" "2" "3"))
; => NIL

If we define the function to use the equality on string test (instead of the default eql):

(member "2" '("1" "2" "3") :test #'string=)
; => (2 3)

If we need this special variant a lot, we can write a macro:

(defmacro string-member (e es)
  `(member ,e ,es :test ,#'string=))

(string-member "2" '("1" "2" "3"))

And we can use expansion to double check if things expand correctly:

(macroexpand '(string-member "x" '("x")))
; => (MEMBER "x" '("x") :TEST #<FUNCTION STRING=>)

Naturally we can expand defmacro:

(macroexpand '(defmacro x (y) `(+ 1 y)))
; I am using SBCL here
;;=> (PROGN
;;  (EVAL-WHEN (:COMPILE-TOPLEVEL)
;;    (SB-C::%COMPILER-DEFMACRO :MACRO-FUNCTION 'X T))
;;  (EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
;;    (SB-C::%DEFMACRO 'X
;;                     (SB-INT:NAMED-LAMBDA (MACRO-FUNCTION X)
;;                         (#:EXPR #:ENV)
;;                       (DECLARE (SB-C::LAMBDA-LIST (Y)))
;;                       (DECLARE (IGNORE #:ENV))
;;                       (SB-INT:NAMED-DS-BIND (:MACRO X . DEFMACRO)
;;                           (Y)
;;                           (CDR #:EXPR)
;;                         (BLOCK X `(+ 1 Y))))
;;                     (SB-C:SOURCE-LOCATION))))
;; T

We now define how a macro works:

(defmacro the-expander (name) `(get ,name 'expander))

The expander is our way of storing macros definition. Given the name of the macro, get will retrieve the =’expander= for the macro.

(defmacro the-defmacro (name params &body body)
  (let ((g (gensym)))
    `(progn
      (setf (the-expander ',name)
       #'(lambda (,g)
           (block ,name
             (destructuring-bind ,params (cdr ,g)
               ,@body))))
      ',name)))

Something cool happens here: we setf the expander for the macro to be a function. This takes a parameter with a random name (gensym) and produces a block initialized with the future parameters. The parameter will be our macro application!

(defun the-macroexpand-1 (expr)
  (if (and (consp expr) (the-expander (car expr)))
      (funcall (the-expander (car expr)) expr)
      expr))

At this point we can expand the macro by executing the function for the given macro application. The if statement filters out the cases where the expression is not a list or no macro expansion is defined for the name.

Finally let’s use the-macroexpand-1 to define our previous string-member macro:

(the-defmacro string-member1 (e es)
  `(member ,e ,es :test ,#'string=))

(the-macroexpand-1 '(string-member1 "x" '("x" "2")))
;; => (MEMBER "x" '("x" "2") :TEST #<FUNCTION STRING=>)

on-lisp-macros's People

Contributors

ag91 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.