Higher Order Functions “I hope you’re convinced, by now, that programming languages with first-class functions let you find more opportunities for abstraction,

Slides:



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

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Functions Robin Burke CSC 358/
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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 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.
Rahman Lavaee Mashhadi Mohammad Shadravan. Conditional expressions LISP was the first language to contain a conditional expression In Fortran and Pascal.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
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.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.
Functional Programming 02 Lists
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Common Lisp! John Paxton Montana State University Summer 2003.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
LISP John McCarthy and Marvin Minsky formed MIT’s AI Project in Suggested reading: John McCarthy’s home page
Functional Programming in Scheme and Lisp.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
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.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
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
Search as a problem solving technique. Consider an AI program that is capable of formulating a desired goal based on the analysis of the current world.
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
ISBN Chapter 15 Functional Programming Languages.
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
The Environment Model*
LISP A brief overview.
J.E. Spragg Mitthögskolan 1997
FP Foundations, Scheme In Text: Chapter 14.
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Functional Programming Concepts
Lisp: Using Functions as Data
LISP A brief overview.
Functional Programming Concepts
Defining Macros in Lisp
Functional Programming: Lisp
Lisp: Using Functions as Data
Functional Programming Concepts
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lisp.
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
Presentation transcript:

Higher Order Functions “I hope you’re convinced, by now, that programming languages with first-class functions let you find more opportunities for abstraction, which means your code is smaller, tighter, more reusable, and more scalable.” —Joel Spolsky

Eval eval is the heart of REPL Takes an expression, evaluates it and returns it >(eval '(+ 2 5)) 7 >(eval (list '* 2 5 7)) 70 >(eval (eval '''eval)) EVAL

Function (#') function returns the object associated with a function name >(function +) # >(defun f (x) (* x x)) F >(function f) (LAMBDA-BLOCK F (X) (* X X)) >(function (lambda (x) (* x x))) (LAMBDA-CLOSURE () () () (X) (* X X)) Exact results depend on implementation (these are GCL) In most contexts, can be abbreviated with #'

Apply Takes a function and a list of arguments for it, and returns the result of applying the function to the arguments Last argument must be a list whose final cdr is nil >(apply #'+ '(2 5 6)) 13 >(apply #'* 2 4 '(3 1)) 24

Funcall Same as apply, but the arguments don’t need to be in a list >(funcall #' ) 16 >(funcall #'cons 'a 'b) (A. B)

Higher Order Functions A Higher Order Function is one that has at least one function as an argument apply and funcall are higher order functions There are many others You can also make your own

Mapcar mapcar is a very useful higher order function Applies a function to each element of a list, one at a time, and returns the list of results (mapcar function list) >(mapcar #'evenp '( )) (NIL T NIL T NIL T NIL) >(mapcar #'oddp '( )) (T NIL T NIL T NIL T)

Define Mapcar (defun my-mapcar (f xs) "Function similar to mapcar" (if (consp xs) (cons (funcall f (car xs)) (my-mapcar f (cdr xs))) nil)) Actual definition is slightly more complicated

Anonymous Functions Lambda abstractions are useful in conjunction with higher order functions Example: the following function subtracts n from each element in a list: >(defun subtract-n (n xs) (mapcar #'(lambda (x) (- x n)) xs)) SUBTRACT-N >(subtract-n 5 '( )) ( )