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.

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
Advertisements

CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Macros Jonathan Bachrach MIT AI Lab. Macro Syntactic extension to core language Defines meaning of one construct in terms of other constructs Their declarations.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Racket Introduction CSC270 Pepper major portions credited to
Macros and Function Generators CS 480/680 – Comparative Languages.
A problem with functions: arguments must always have values that can be worked out Whenever we call a function we give it arguments. Lisp then works out.
10 Macros.  Lisp code is expressed as lists, which are Lisp objects  This makes it possible to write programs that would write programs  This lecture.
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.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
PRACTICAL COMMON LISP Peter Seibel 1.
Logical functions and - Logical AND function. Example: (and (> 5 4) (< 5 4)) or - Logical OR function. Example: (and (> 5 4) (< 5 4)) not - Logical negation.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
Natural deduction of logical proofs Kalish & Montegue: a set of heuristics for doing logical proofs Introduction rules –not introduction, and introduction,
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Introduction to Lisp For Scheme Users. What Makes Lisp Different? Built-in Support for Lists Automatic Storage Management Dynamic Typing First-Class Functions.
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.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Clojure 2 Feb 7,
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
Functional Programming in Scheme and Lisp.
Lecture 1-2CS251: Intro to AI/Lisp II “And now for something completely different…”
Feb 7, 2015 Thinking in Clojure. Jumping in We’ll quickly go through Clojure’s data types, some basic functions, and basic syntax Then we’ll get to the.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
Macros You might recall from earlier that some of the statements we use are not actually functions but macros –In CL, a function evaluates all of its parameters.
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem Solver has 2 basic components:  Search engine (uses.
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
Lecture 2-1CS251: Intro to AI/Lisp II Today Quiz Announcements –Allegro for Windows checkout –Guest lecture on Thursday Today’s topics –More on macros.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
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.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Defining Macros in Lisp
Case Study: Undefined Variables
Functions and Macros.
slides created by Marty Stepp
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
Programming in JavaScript
The Argumentative Essay A Review
The Metacircular Evaluator (Continued)
CSE S. Tanimoto Explicit Function Application
6.001 SICP Further Variations on a Scheme
Lisp and Scheme I.
Lisp: Using Functions as Data
Lecture 5: Basic Java Syntax AP Computer Science Principles
6.001 SICP Variations on a Scheme
Programming in JavaScript
Defining Macros in Lisp
Clojure Macros.
Clojure 2 22-Apr-19.
Lisp: Using Functions as Data
Modern Programming Languages Lecture 18 Fakhar Lodhi
topics interpreters meta-linguistic abstraction eval and apply
To understand recursion, one must first understand recursion.
Common Lisp II.
Computer Science 340 Software Design & Testing
Thinking in Clojure 8-Jun-19.
Defining Macros in Scheme
Presentation transcript:

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 Clojure Macros...

Transforming code (read-string “( )”) (class (read-string “( )”)) (eval (read-string “( )”)) (class (eval (read-string “( )”))) (let [expression] (read-string “( )”)] (cons (read-string “*”) (rest expression))) (let [expression] (quote ( ))] (cons (quote *) (rest expression))) ‘( )

Our first macro (defmacro when “Evaluates test. If logical true, evaluates body.” [test & body] (list ‘if test (cons ‘do body))) (when (= 2 (+ 1 1)) (print “You got”) (print “ the touch!”) (println))

First macro continued (list ‘if ‘(= 2 (+ 1 1)) (cons ‘do ‘((print “You got”) (print “ the touch!”) (println)))) (if (= 2 (+ 1 1)) (do (print “You got”) (print “ the touch!”) (println)))

Another example (defmacro cond “Long comment here.” [& clauses] (when clauses (list ‘if (first clauses) (if (next clauses) (second clauses) (throw (IllegalArgumentException. “cond requires an even number of forms”))) (cons ‘clojure.core/cond (next (next clauses))))))

macroexpand-1 (macroexpand-1 ‘(when (= 1 2) (println “math is broken”))) (macroexpand-1 nil) (defmacro broken-when [test & body] (list test (cons ‘do body))) Use macroexpand-1 to figure out why this doesn’t work. Note that macroexpand-1 expands one level.

macroexpand (defmacro when-falsy [test & body] (list ‘when (list ‘not test) (cons ‘do body))) (macroexpand-1 ‘(when-falsy (= 1 2) (println “hi”))) (macroexpand ‘(when-falsy (= 1 2) (println “hi”)))

assert (defmacro assert [x] (when *assert* ;; make sure enabled (list ‘when-not x (list ‘throw (list ‘new AssertionError (list ‘str “Assert failed: “ (list ‘pr-str (list ‘quote x)))))))) (assert (= 1 2)) Real assert is even more complicated.

Assert with syntax quote (defmacro assert [x] (when *assert* `(when-not ~x (throw (new AssertionError (str “Assert failed: “ (pr-str ‘~x))))))) Before when-not above is a back-quote (aka a syntax quote). It selectively quotes all but things with ~ or

Syntax quote (def a 4) ‘(1 2 3 a 5) (list a 5) `(1 2 3 ~a 5) The book says syntax quote (`)is a little cockeyed and ready to party. Unquote (~) is the thing that evaluates inside of syntax quoted code/data. Unquote-splice is like unquote, but it splices in the result.

Unquote-splice (def other-numbers ‘( )) `(1 2 3 ~other-numbers 9 10) (concat ‘(1 2 3) other-numbers ‘( 9 10)) `( )

Symbol capture (def y 100) (defmacro make-adder [x] `(fn [~’y] (+ ~x ~’y))) ((make-adder (+ y 3)) 5) Fix the problem with gensym (gensym) generates a unique new symbol

Symbol capture cont. (defmacro make-adder [x] (let [y (gensym)] `(fn [~y] (+ ~x ~y)))) Auto-gensym makes this more concise. (defmacro make-adder [x] `(fn [y#] (+ ~x y#)))

Another example (defmacro and ([] true) ([x] x) ([x & next] `(let [and# ~x] (if and# (and and#))))

More detail about and Version that isn’t quite right... (defmacro our-and [x] ([] true) ([x] x) ([x & next] `(if ~x (our-and ~x))) (our-and (do (println “hi there”) (= 1 2)) (= 3 4)) Prints “hi there” twice.