Lisp: Using Functions as Data

Slides:



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

Functions Robin Burke CSC 358/
ANSI Common Lisp 5. Control 16 June Blocks -- progn progn > (progn (format t “a”) (format t “b”) ( )) ab 23 The expressions within its body.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
Returning values from functions You can return a value from a function by using the built- in function : ( return-from Function_name value) For example:
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Digital Electronics Data Structures LISP
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
CSE (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Functions CSC 358/
Section 15.4, 15.6 plus other materials
Lisp S-Expressions: ATOMs
Defining Macros in Lisp
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
The Environment Model*
Using Lisp Lisp is a interactive system
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Scheme: Basic Functionality
Bindings, Scope, and Extent
Abstraction and Repetition
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
Functional Programming Concepts
Lisp: Using Functions as Data
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Functional Programming Concepts
Lisp: Representation of Data
Defining Macros in Lisp
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Bindings, Scope, and Extent
LISP: Basic Functionality
Modern Programming Languages Lecture 18 Fakhar Lodhi
Functional Programming Concepts
LISP: Basic Functionality
Bindings, Scope, and Extent
Common Lisp II.
LISP: Basic Functionality
More Scheme CS 331.
Defining Macros in Scheme
Lisp: Representation of Data
LISP primitives on sequences
Abstraction and Repetition
Presentation transcript:

Lisp: Using Functions as Data APPLY MAPCAR Anonymous functions using LAMBDA Closures Functions that create closures EVAL CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions. They can then be explicitly applied. ; Implicit application of + > (+ 1 2 3) 6 ; Explicit application of + > (apply (function +) (list 1 2 3)) CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data Functional Arguments A functional object or functional argument is a representation of a function that can be passed as an argument and applied to its own arguments. ; Example > (function car) #<Function CAR> > #’car These used to be called funargs but are now usually called “closures”. CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Choosing a function to apply > (defun do-command (command arglist) (let (f) (if (equal command '(multiply that)) (setq f #'*) (setq f #'+) ) (apply f arglist) ) DO-COMMAND > (do-command '(add them) '(8 13)) 21 CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Applying a Function to Successive Elements of a List ; Here’s a unary function with a funny name: > (1+ 20) 21 ; Here’s custom-made recursive function: > (defun increment-each (lst) (if (null lst) nil (cons (1+ (first lst)) (increment-each (rest lst)) ) ) ) INCREMENT-EACH > (increment-each '(1 2 3 4 99.9)) (2 3 4 5 100.9) CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Applying a Function to Successive Elements of a List (the MAPCAR way) ; Here’s a simpler way: > (mapcar #'1+ '(1 2 3 4 99.9)) (2 3 4 5 100.9) ; OK for functions that take multiple args: > (mapcar #'cons '(a b c) '(1 2 3)) ((A . 1) (B . 2) (C . 3)) CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Anonymous Functions with LAMBDA ; Creating and using a named function: > (defun cube (x) (* x x x)) CUBE > (cube 5) 125 ; Creating and using an unnamed function: > ((lambda (x) (* x x x)) 5) Benefits of unnamed functions: -- Can help achieve locality of reference, -- Might help keep the name space “unpolluted” CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data Closures A closure is a callable functional object that can use variable bindings in effect when the closure was created. > (setq toggle (let ((bit 0)) #'(lambda () (setq bit (- 1 bit))) ) ) #<Interpreted Closure (unnamed) @ #x204c8fca> > (funcall toggle) 1 CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

A Function that Makes Closures > (defun make-toggle (on-value off-value) (let ((bit 0)) #'(lambda () (setq bit (- 1 bit)) (if (= bit 1) on-value off-value) ) ) ) MAKE-TOGGLE > (setq traffic-light (make-toggle 'green 'red) ) #<Interpreted Closure (unnamed) @ #x204c8fcb> > (funcall traffic-light) RED GREEN CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Calling MAKE-TOGGLE (continued) > (setq tide-change (make-toggle 'high-tide 'low-tide) ) #<Interpreted Closure (unnamed) @ #x204c8fcc> > (funcall tide-change) LOW-TIDE HIGH-TIDE > (funcall traffic-light) RED CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Closures that Share Bindings > (defun make-stack () (let ((stack nil)) (cons #'(lambda (item) ; closure for push. (setq stack (cons item stack)) ) #'(lambda () ; closure for pop. (if stack (progn (let ((top (first stack))) (setq stack (rest stack)) top) ) nil) ) ) ) ) CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Closures that Share Bindings > (setq my-stack-closures (make-stack)) (#<CLOSURE #x204cde> . #<CLOSURE #x204cdf>) > (funcall (car my-stack-closures) 'apple) (APPLE) > (funcall (car my-stack-closures) 'pear) (PEAR APPLE) > (funcall (cdr my-stack-closures)) PEAR CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data Evaluation If FORM is a constant, return FORM itself. If FORM is a non-constant symbol, and this evaluation is taking place within the scope of a lexical binding for it, return the value of the topmost lexical binding for it, and if not in the scope of a lexical binding, return the symbol-value of FORM if any, and if none, issue an unbound variable error. IF FORM is not a list, issue an error. IF the first element of FORM, which now must be a list, is the name of a function, or is a LAMBDA expression, recursively evaluate the remaining elements of the list as arguments, and then apply the function to the evaluated arguments, and return the result. (But give an error if the wrong number of args were given). IF the first element of FORM is the name of a special form, then perform the special evaluation required by that form and return its value. IF the first element of FORM is the name of a macro, apply MACROEXPAND to the form, and then evaluate the result of that and return whatever results from this second evaluation. OTHERWISE issue an error saying that the first element of FORM was not recognized as a function. CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Calling the Evaluator Explicitly > (setq fruit 'apple) APPLE > (setq apple 'red) RED > fruit > (eval fruit) > (eval 'fruit) CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

Calling the Evaluator Explicitly (Cont.) > (setq exp '(+ 3 4 (* 5 6) 7 8)) (+ 3 4 (* 5 6) 7 8) > (eval exp) 52 > (eval (cons '* (rest exp))) 20160 ; = (* 3 4 (* 5 6) 7 8) CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data A Caveat with EVAL EVAL does not recognize lexical bindings: > (let ((x 5) (exp2 '(* 2 3 4 x))) (print exp2) (print (eval exp2)) ) Error: Attempt to take the value of the unbound variable 'X'. CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data

A Caveat with EVAL (Cont.) EVAL does recognize dynamic bindings: > (setq x 0) > (let ((x 5) (exp2 '(* 2 3 4 x))) (print exp2) (print (eval exp2)) ) The evaluation of (* 2 3 4 X) takes place within the body of EVAL (a built-in function) which is OUTSIDE the lexical scope established by the LET form. But the global value of X is accessible and is used. CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data