Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
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.
MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Chapter 6: User-Defined Functions I
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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:
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
How to load a program file? Lisp programs in Allegro are saved under the file extension.cl. To load a file into the Lisp console, use the following: (load.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
The switch Statement, DecimalFormat, and Introduction to Looping
The different kinds of variables in a Java program.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
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.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Object Oriented Programming
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Chapter 6: User-Defined Functions
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Learners Support Publications Classes and Objects.
CPS120: Introduction to Computer Science Decision Making in Programs.
Controlling Execution Dong Shao, Nanjing Unviersity.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
PRACTICAL COMMON LISP Peter Seibel 1.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
Bordoloi and Bock CONTROL STRUCTURES: CONDITIONAL CONTROLS.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Principles of programming languages 4: Parameter passing, Scope rules
User-Defined Functions
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Lecture 18 Arrays and Pointer Arithmetic
Classes and Objects.
Bindings, Scope, and Extent
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
6.001 SICP Variations on a Scheme
CISC101 Reminders All assignments are now posted.
Peter Seibel Practical Common Lisp Peter Seibel
Lisp: Using Functions as Data
Bindings, Scope, and Extent
Classes, Objects and Methods
CSE 341 Lecture 11 b closures; scoping rules
Bindings, Scope, and Extent
Common Lisp II.
Scope Scope is the space within which a variable label exists and can be used. Python talks of names being bound to the code in an area and that determining.
Rules of evaluation The value of a number is itself.
„Lambda expressions, Optional”
Parameters and Arguments
Presentation transcript:

Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall

The apply-append trick Suppose we have a list L of lists and we wish to join all the lists in L end-to-end. For example suppose L is: ((a f g) (c d) ( ) (p q r)) and we want a function that will essentially “remove the first-level brackets” to give: (a f g c d p q r) All we need is: (apply #’append L)

LAMBDA So far, we have seen two places in which a symbol can be used to refer to a function: 1.As the first item in a list representing a function call: (append x 12) 2.With (function …) wrapped round it when acting as the argument to another function: (apply #’* (find-numbers)) What can actually appear in these positions is either a symbol which is the name of a function or a description of an unnamed function. The latter effect is achieved using a lambda definition. A lambda definition is just like an ordinary function definition, except that:  There is no function name specified  The word lambda is used instead of defun

LAMBDA (lambda (L) (first (rest L)) > ((lambda (L) (first (rest L))) ‘(a b c d e)) b >(funcall #’(lambda (L) (first (rest L))) ‘(a b c d e)) b >(apply #’(lambda (L) (first (rest L))) ‘((a b c d e))) b Note: 1. The #’ is not part of the lambda definition, but has to be stuck on the front just when the lambda expression is being passed as an argument to another function 2. When the lambda expression appears in the privileged first position, it does not need the #’, just as the symbolic name of a function does not need it; however, when the lambda expression appears as an argument to another function, it does need the #’.

>((lambda (A B) (* (+ A B) (-A B) ) ) 10 3) 91 >(funcall #’ (lambda (A B) (* (+ A B) (-A B) ) ) 10 3) 91 >(apply #’ (lambda (A B) (* (+ A B) (-A B) ) ) ‘(10 3)) 91

What is lambda for: 1.When the function definition is specific to a particular situation, and so can be defined and used in just one place. Usually the motive for forming the computation into a function is that it is being passed in as argument to some other functions which will use that computation in various ways, probably applying it repeatedly 2.When there is a need to manipulate variable names so that certain variables are “in scope” at the right time 3.When a function is constructing a function which it will then return as its result; that is, the result of some computation is not merely a data-structure, but is a new function definition which can then be passed on and used.

Consider this EXAMPLE: (defun fncomp (fn1 fn2) #’ (lambda (val) (funcall fn1 (funcall fn2 val)) ) >(setf g (fncomp #’first #’rest)) This function returns a function as its answer!!!!! g is now the function returned by fncomp g = #’(lambda (val) (funcall #’first (funcall #’rest val)) > (funcall g ‘(a b c d)) b

Reminder: LET Although strict functional programming does not allow the use of assignment to local variables, Common Lisp has a command which gives a limited (but very useful) kind of variable declaration and initialization which does not violate the principles of functional programming. The let command: –Declares one or more local variables –Optionally gives them initially values –Executes a sequence of S-expressions using these settings

The general format of LET command: (let (( ) ( ) … ( ) ) … )

EXAMPLE (defun example(x y) (let( (a (factorial x)) ) ( + a (power y a)) ) ) A function to work out (factorial x) + y (factorial x) Because we have to use the factorial value twice, we use a let to hold that value in a variable called a.

LET and LAMBDA A LET command can be thought of as directly equivalent to the use of a lambda expression. A LET statement, introduces variables, gives them initial values, evaluates some expressions, then discards the variables; this is essentially what happens when a lambda function is defined and then used immediately Compare: (let ((x 3) (y 4)) (+ (*x y) (+ x y)) With: ((lambda (x y) (+ (*x y ) (+ x y))) 3 4)

Example with Lambda (defun example(x y) ( (lambda(a) ( + a (power y a))) (factorial x) ) ) Because we have to use the factorial value twice, define a lambda function and give it (factorial x) as its argument. That argument can then be used inside the lambda function twice. A function to work out (factorial x) + y (factorial x)

Hence, using LET does not go beyond the principles of functional programming, since it is simply a handy notation for defining and then immediately using a LAMBDA expression. However, do not use LAMBA function calls simply to introduce and bind local variables – the examples here are merely to illustrate a conceptual point. The natural way to introduce local variables and give them initial values is the LET command; trying to do it with LAMBDA will be rather complicated.

LET = simultaneous assignment In the LET command the initial values of variables are all assigned simultaneously, so the initialization of one variable cannot assume the value of one of the other variables in the same header. Hence in: (let ( (x (rest y)) (z (first x)) ) … ) The expression (first x) will refer to some x which is available, outside the LET command, not to the x which is alongside it in the LET statement

LET* The LET* introduces and initializes the variables one after another (sequentially) so that variables can be used as soon as they are set up, even in further initializations: > (setf x 3) 3 > (let ((a x) (x 5) (b x)) (list a x b)) (3 5 3) > (let* ((a x) (x 5) (b x)) (list a x b)) (3 5 5)

Bound and free variables: The LET & LAMBDA constructs are examples of statements which can introduce new variables with a clearly defined (and limited) scope. A variable is said to be bound within the area of program to which it belongs; in the case of LET command, that area is what is called the “body” of the LET command. Any variable mentioned in a piece of program which is not “bound”within that piece of program text is said to be free in that text. For example consider the following function:

If we were to consider the LET block in isolation, the variable x is free with respect to that block. However, x is bound with respect to the function example. (its bound in the argument to the function) (defun example(x y) (let( (a (factorial x)) ) ( + a (power y a)) )

LEXICAL SCOPING Common Lisp relies on what is known as lexical scoping which means, roughly, that a variable may be referred to at any point in the program which is textually enclosed by the construct which introduced the variable. If there are several enclosing statements and if two of these have introduced variables with the same name, it is the innermost one which is taken as being referred to. Try the next examples:

>(let((x 6) (y 7)) (print x) (print y) (let((x 10) (z 8)) (print x) (print y) (print z) ) (print x) ) print is a built in lisp function that prints something directly on the console, rather than returning it as a value. I just use it here to make a point. > Notice that when we first print x, we get 6 (inside the outer let), but when we next print x (inside the inner let) we get 10. Finally, when we print x in the outer let again, we get 6 again Similar nesting rules apply if there is a lambda function inside another lambda function