CSE 341, S. Tanimoto Lisp 5 - 1 Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.

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.
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.
1 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
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.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
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
Functional Programming: Lisp MacLennan Chapter 10.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
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
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
Lisp: Using Functions as Data
Bindings, Scope, and Extent
LISP: Basic Functionality
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
Presentation transcript:

CSE 341, S. Tanimoto Lisp 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 + > ( ) 6 ; Explicit application of + > (apply (function +) (list 1 2 3)) 6

CSE 341, S. Tanimoto Lisp 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) # > #’car # These used to be called funargs but are now usually called “closures”.

CSE 341, S. Tanimoto Lisp 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 341, S. Tanimoto Lisp 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 '( )) ( )

CSE 341, S. Tanimoto Lisp Applying a Function to Successive Elements of a List (the MAPCAR way) ; Here’s a simpler way: > (mapcar #'1+ '( )) ( ) ; OK for functions that take multiple args: > (mapcar #'cons '(a b c) '(1 2 3)) ((A. 1) (B. 2) (C. 3))

CSE 341, S. Tanimoto Lisp 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) 125 Benefits of unnamed functions: -- Can help achieve locality of reference, -- Might help keep the name space “unpolluted”

CSE 341, S. Tanimoto Lisp 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))) ) ) # > (funcall toggle) 1 > (funcall toggle) 0

CSE 341, S. Tanimoto Lisp 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) ) # > (funcall traffic-light) RED > (funcall traffic-light) GREEN

CSE 341, S. Tanimoto Lisp Calling MAKE-TOGGLE (continued) > (setq tide-change (make-toggle 'high-tide 'low-tide) ) # > (funcall tide-change) LOW-TIDE > (funcall tide-change) HIGH-TIDE > (funcall traffic-light) RED > (funcall tide-change) LOW-TIDE

CSE 341, S. Tanimoto Lisp 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 341, S. Tanimoto Lisp Closures that Share Bindings > (setq my-stack-closures (make-stack)) (#. # ) > (funcall (car my-stack-closures) 'apple) (APPLE) > (funcall (car my-stack-closures) 'pear) (PEAR APPLE) > (funcall (cdr my-stack-closures)) PEAR

CSE 341, S. Tanimoto Lisp 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 341, S. Tanimoto Lisp Calling the Evaluator Explicitly > (setq fruit 'apple) APPLE > (setq apple 'red) RED > fruit APPLE > (eval fruit) RED > (eval 'fruit) APPLE

CSE 341, S. Tanimoto Lisp 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))) ; = (* 3 4 (* 5 6) 7 8)

CSE 341, S. Tanimoto Lisp A Caveat with EVAL EVAL does not recognize lexical bindings: > (let ((x 5) (exp2 '(* x))) (print exp2) (print (eval exp2)) ) Error: Attempt to take the value of the unbound variable 'X'.

CSE 341, S. Tanimoto Lisp A Caveat with EVAL (Cont.) EVAL does recognize dynamic bindings: > (setq x 0) 0 > (let ((x 5) (exp2 '(* x))) (print exp2) (print (eval exp2)) ) 0 The evaluation of (* 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.