Functions and Macros.

Slides:



Advertisements
Similar presentations
Closures & Environments CS153: Compilers Greg Morrisett.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Macros and Function Generators CS 480/680 – Comparative Languages.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PRACTICAL COMMON LISP Peter Seibel 1.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
Functional Programming in Scheme
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 in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Clojure 2 Feb 7,
Functional Programming in Scheme and Lisp.
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.
PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Clojure in the Cloud Everett Toews Developer JavaOne Sept. 29, 12:30 pm.
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
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.
Lets and Loops Tail Recursion.
Functional Programming Languages
Defining Macros in Lisp
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Scheme : variant of LISP
Lisp naturally embedded in Ruby
Pre-processor Directives
Higher-Order Procedures
Clojure 4 Sequences 27-Nov-18.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
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.
(early slides assume map/filter)
Scheme: Basic Functionality
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
Adapted from slides by Nicholas Shahan and Dan Grossman
Lisp, Then and Now.
6.001 SICP Variations on a Scheme
Defining Macros in Lisp
Clojure Macros.
Announcements Quiz 5 HW6 due October 23
Peter Seibel Practical Common Lisp Peter Seibel
Clojure 2 22-Apr-19.
Lisp: Using Functions as Data
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Clojure 3 1-Jun-19.
Rules of evaluation The value of a number is itself.
Defining Macros in Scheme
Presentation transcript:

Functions and Macros

Function Definition Forms (defn func [arg1 arg2 arg3] (println arg1 arg2 arg3))

Function Definition Forms (defn func [arg1 arg2 arg3] (println arg1 arg2 arg3)) (def func (fn [arg1 arg2 arg3] (println arg1 arg2 arg3)))

Function Definition Forms (defn func [arg1 arg2 arg3] (println arg1 arg2 arg3)) (def func (fn [arg1 arg2 arg3] (println arg1 arg2 arg3))) (def func #(println %1 %2 %3))

Variadic functions Functions can take an arbitrary number of arguments (fn [arg1 arg2 & moreArgs] (printf arg1 arg2 moreArgs)) Functions can take an arbitrary number of arguments The built-in function + does so The moreArgs parameter is a list of the additional arguments Functions can be written to take zero or more arguments with (fn [& moreArgs] (printf arg1 arg2 moreArgs))

Functions with different numbers of arguments (defn func ([x] (println "got one")) ([x y] (println "got two")))

Macros vs. Functions Functions Macros Execute at run time Evaluate all arguments before calling the function Produce values Are first-class values at runtime can be composed can be mapped over containers Execute at compile time Don't evaluate arguments, just pass the code Produce code Do not exist at runtime compose only with other macros (viral) cannot be mapped

Macros are generally discouraged At least, prefer functions over macros

When to use macros? The code must run at compile time You need access to un-evaluated arguments Code must be emitted inline

When to use macros? The code must run at compile time expensive fixed calculations capturing build time / parameters

When to use macros? You need access to un-evaluated arguments this is the most common case short-circuiting logic and / or definition of code (implicit lambdas)

When to use macros? Code must be emitted inline calling logging commands that capture the line number

Macro definition forms (defmacro macro [arg1 arg2 & args] (...) ...)

Macro definition forms (defmacro macro [arg1 arg2 & args] (...) ...) (defmacro macro ([] "no arguments") ([arg1 arg2 & args] (...) ...))

Example Macro: Scheme-style function definition Scheme function definitions look a bit different from those of LISP and Clojure. (define (func arg1 arg2 arg3) ...) as opposed to (defn func [arg1 arg2 arg3] ...)

Example Macro: Scheme-style function definition Let's try to create a macro to emulate this (defmacro define [name-and-args & body] ...)

Example Macro: Scheme-style function definition Let's try to create a macro to emulate this (defmacro define [name-and-args & body] (let [name (first name-and-args) args (rest name-and-args)] (list 'defn name args body))

Example Macro: Scheme-style function definition Let's try to create a macro to emulate this (defmacro define [name-and-args & body] (let [name (first name-and-args) args (rest name-and-args)] (list 'defn name args body)) That's no good! args needs to be a vector!

Example Macro: Scheme-style function definition Let's try to create a macro to emulate this (defmacro define [name-and-args & body] (let [name (first name-and-args) args (rest name-and-args)] (list 'defn name (into [] args) body))

Example Macro: Scheme-style function definition Let's try to create a macro to emulate this (defmacro define [name-and-args & body] (let [name (first name-and-args) args (rest name-and-args)] (list 'defn name (into [] args) body)) Better, but still not quite right. The body is wrapped in an extra set of parentheses!

Example Macro: Scheme-style function definition Let's try to create a macro to emulate this (defmacro define [name-and-args & body] (let [name (first name-and-args) args (rest name-and-args)] (list 'defn name (into [] args) (cons 'do body)))

Syntax quote Rather than explicitly building the lists, it can be cleaner to use a special kind of quoted expression `(symbol ~evaluated-symbol ~@flattened-list)

NOTE: It is denoted by a back-tick rather than a single quote Syntax quote Rather than explicitly building the lists, it can be cleaner to use a special kind of quoted expression `(symbol ~evaluated-symbol ~@flattened-list) NOTE: It is denoted by a back-tick rather than a single quote

NOTE: It is denoted by a back-tick rather than a single quote Syntax quote Rather than explicitly building the lists, it can be cleaner to use a special kind of quoted expression `(symbol ~evaluated-symbol ~@flattened-list) NOTE: It is denoted by a back-tick rather than a single quote ~symbol is replaced by the value associated with the symbol at compile time

Syntax quote Rather than explicitly building the lists, it can be cleaner to use a special kind of quoted expression `(symbol ~evaluated-symbol ~@flattened-list) NOTE: It is denoted by a back-tick rather than a single quote ~symbol is replaced by the value associated with the symbol at compile time ~@symbol splices the contents of the symbol into the surrounding list