Clojure Macros.

Slides:



Advertisements
Similar presentations
Macros and Function Generators CS 480/680 – Comparative Languages.
Advertisements

LISP Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
PRACTICAL COMMON LISP Peter Seibel 1.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
CS 355 – PROGRAMMING LANGUAGES Dr. X. Common LISP A combination of many of the features of the popular dialects of LISP around in the early 1980s A large.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Clojure 4 Sequences 20-Oct-15. Clojure errors (NO_SOURCE_FILE:12) Useless--just means you’re running from the REPL shell java.lang.Exception: EOF while.
Machine Independent Macro Processor Features Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro.
CPS120: Introduction to Computer Science Functions.
Lecture 1-2CS251: Intro to AI/Lisp II “And now for something completely different…”
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
1 COSC generating functions, templates, and macros Yves Lespérance Adapted from Peter Roosen-Runge.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
PRACTICAL COMMON LISP Peter Seibel 1.
Macros CSC 358/ Outline  Homework #6  Macro Intro  Backquote  Macros  Modify macros  Read Macros.
Programming In Python. Starter Using the internet… Find what a programming language is.
Clojure Macros. Homoiconicity All versions of Lisp, including Clojure, are homoiconic This means that there is no difference between the form of the data.
Macros Forms that the compiler expands into code ● Decide if the macro is really necessary ● Write down the syntax of the macro ● Figure out what the macro.
Feb 17, 2015 Clojure 4. Macros Code is data We have heard this before. It is what makes Lisp so amenable to the use of macros. Examples from Mastering.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CHAPTER NINE.
Component 1.6.
Chapter 7: Function.
Assembler, Compiler, MIPS simulator
Conditional Expressions
Topics Designing a Program Input, Processing, and Output
Tail Recursion.
Defining Macros in Lisp
CS510 Compiler Lecture 4.
CSE341: Programming Languages Lecture 15 Macros
C Programming Tutorial – Part I
Pre-processor Directives
Emily Leland (Not Nick) Spring 2017
CSE341: Programming Languages Lecture 15 Macros
Important Concepts from Clojure
Important Concepts from Clojure
Preparing for MUPL! Justin Harjanto
Clojure 4 Sequences 27-Nov-18.
Functions and Macros.
Clojure 4 Sequences 5-Dec-18.
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
CSE341: Programming Languages Lecture 15 Macros
Adapted from slides by Nicholas Shahan and Dan Grossman
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Defining Macros in Lisp
CSE341: Programming Languages Lecture 15 Macros
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
Topics Designing a Program Input, Processing, and Output
Lecture 8 Programming Paradigm & Languages. Programming Languages The process of telling the computer what to do Also known as coding.
Nicholas Shahan Spring 2016
Important Concepts from Clojure
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
CSE341: Programming Languages Lecture 15 Macros
topics interpreters meta-linguistic abstraction eval and apply
Common Lisp II.
Clojure 3 1-Jun-19.
Lisp.
Monads.
Defining Macros in Scheme
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 15 Macros
Procedures & Macros Introduction Syntax Difference.
Presentation transcript:

Clojure Macros

Macros, defined Metaprogramming is writing code that produces code Metaprogramming is particularly easy in the Lisp family of languages, because of homoiconicity “All code is data, and all data is code” In Lisp languages, macros are the primary means of doing metaprogramming A macro definition is like a function definition, with three important differences Arguments to a macro are not evaluated Macro calls are evaluated at compile time The return value of a macro should be executable code

A trivial macro: triple-do (defmacro triple-do [form] (list 'do form form form) ) The do is quoted, so it is put as is into the result list Three copies of the value passed in to the form are put into the list (triple-do (println "Hello")) Result: The list (do (println "Hello") (println "Hello") (println "Hello")) If executed from the REPL, the result is Hello Hello Hello nil

Quotes and unquotes The usual way of quoting something is to put a single quote mark in front of it: '(a b c) The backquote does the exact same thing: `(a b c) However, things within a backquote can be “unquoted” and evaluated, by putting a tilde, ~, in front of them Quotes and unquotes can be nested, to any level The following are equivalent: (defmacro triple-do [form] (list 'do form form form) ) (defmacro triple-do [form] `(do ~form ~form ~form) ) The second of these is called a “template” For complex macros, templates can be a lot easier to read, because they “look like” the code that is generated

Splicing unquotes println can take multiple arguments, which it prints in order Suppose you wanted to write a macro that prints its arguments in reverse order You might try (defmacro rev-println [args] `(println ~(reverse args)) ) Given a list of values, this will print them as a list (rev-println `(a b c)) would print (c b a), not c b a The splicing-unquote operator, ~@, will insert the list values individually, not as a list (defmacro rev-println [args] `(println ~@(reverse args)) )

Generating symbols Just as in a function, you can use let to define the names of local variables These names can be used in the generated code, where they will appear as written However, the names in the generated code may conflict with other names in use where the macro is called To avoid this issue: in any syntax-quoted form (forms using the back tick), add the # symbol to the end of any local names Clojure will replace these names with unique, generated names Example: A println macro that also returns the value printed (defmacro debug-println [expr] `(let [result# ~expr] (println (str “Value is: “ result#)) result# ) )

Debugging To see what you get as the result of expanding a macro, use macroexpand Example: (macroexpand '(triple-do (println "Hello"))) gives (do (println "Hello") (println "Hello") (println "Hello"))

When to use macros General rule: If you can do it with a function, do it with a function Macros are easy to describe but difficult to reason about The primary thing that macros do for you is allow you to modify the language by creating new control structures and formalizing recurring patterns

The End