# Lambda Calculus see notes on Lambda Calculus

Size: px
Start display at page:

Transcription

1 Lambda Calculus see notes on Lambda Calculus Shakil M. Khan adapted from Gunnar Gotshalks recap so far: Lisp data structures basic Lisp programming bound/free variables, scope of variables Lisp symbols, property lists control over evaluation and function application today: Lambda calculus pure functional programming CSE-3401-May

2 λ-calculus history developed by Alonzo Church during 1930 s-40 s one fundamental goal was to describe what can be computed full definition of λ-calculus is equivalent in power to a Turing machine Turing machines and λ-calculus are alternate descriptions of our understanding of what is computable CSE-3401-May λ-calculus history (cont d in the mid to late 1950 s, John McCarthy developed Lisp a programming language based on λ-calculus implementation includes syntactic sugar functions and forms that do not add to the power of what we can compute but make programs simpler and easier to understand CSE-3401-May

3 λ-calculus basis mathematical theory for anonymous functions functions that have not been bound to names present a subset of full definition to get the flavor notation and interpretation scheme identifies functions and their application to operands argument-parameter binding clearly indicates which variables are free and which are bound CSE-3401-May bound and free variables bound variables are similar to local variables in Java function (or any procedural language changing the name of a bound variable (consistently does not change the semantics (meaning of a function free variables are similar to global variables in Java function (or any procedural language changing the name of a free variable normally changes the semantics (meaning of a function CSE-3401-May

4 λ-functions consider following expression ( u + 1 ( u 1 is u bound or free? disambiguate the expression with the following λ-function (λ u. ( u + 1 ( u 1 bound variables defining form clearly indicates that u is a bound variable note the parallel with programming language functions functionname ( arguments { function definition } it seems obvious now but that is because programming languages developed out of these mathematical notions CSE-3401-May λ-functions (cont d consider the following expression ( u + a ( u + b can have any of the following functions, depending on what you mean ( λ u. ( u + a ( u + b u is bound, a and b are free (defined in the enclosing context ( λ u, b. ( u + a ( u + b u and b are bound, a is free ( λ u, a, b. ( u + a ( u + b u, a and b are all bound, no free variables in the expression CSE-3401-May

5 function application functions are applied to arguments in a list immediately following the λ-function { λ u. ( u + 1 ( u + 2 } [ 3 ] 3 u then ==> (3 + 1 ( { λ u. ( u + a ( u + b } [ 7 1 ] 7 1 u then ==> ( 6 + a ( 6 + b and no further in this context {λ u, v. ( u v ( u + v } [ 2p + q, 2p - q ] ==> ( (2p+q (2p - q ( (2p + q + (2p q can pass expressions to a variable can use different bracketing symbols for visual clarity; they all mean the same thing. CSE-3401-May using auxiliary definitions build up longer definitions with auxiliary definitions define u / ( u + 5 where u = a ( a + 1 where a = 7 3 { λ u. u / ( u + 5 } [ { λ a. a ( a + 1 } [ 7 3 ] ] note the nested function definition and argument application ==> { λ u. u / ( u + 5 } [ 4 ( ] { 20 / ( } 0.8 CSE-3401-May

6 functions are variables define f ( 3 + f ( 5 where f ( x = a x ( a + x where a = 4 { λ f. f (3 + f (5 } [ { λ a. { λ x. a x ( a + x } } [ 4 ] ] arguments must be evaluated first ==> { λ f. f (3 + f (5 } [ { λ x. 4 x ( 4 + x } ] { λ x. 4 x (4 + x } (3 + { λ x. 4 x (4 + x } (5 4 * 3 ( * 5 ( ==> 264 CSE-3401-May Lambda notation in Lisp Lambda expressions are a direct analogue of λ- calculus expressions they are the basis of Lisp functions a modified syntax to simplify the interpreter e.g. ( defun double ( x ( + x x is the named version of the following unnamed lambda expression ( lambda ( x ( + x x { λ x. ( x + x } note the similar syntax with λ-calculus and the change to prefix, from infix, to permit a uniform syntax for functions of any number of arguments CSE-3401-May

7 anonymous functions recall in the abstraction for sumint we defined support functions to handle each case (defun double (int (+ int int (defun square (int (* int int (defun identity (int int this adds additional symbols we may not want, especially if the function is to be used only once using lambda we get the same effect without adding symbols (sumint # (lambda (int (+ int int 10 (sumint # (lambda (int (* int int 10 (sumint # (lambda (int int 10 CSE-3401-May the function function what is the meaning of # in the following (sumint # (lambda (int (+ int int 10 it is a short hand # (... (function (... one of its attributes is it works like quote, in that its argument is not evaluated, thus, in this simple context the following will also work (sumint (lambda (int (+ int int 10 later we will see another attribute of function that makes it different from quote whenever a function is to be quoted use # in place of CSE-3401-May

8 recursion recursion with lambda functions uses labels to temporarily name a function the following is a general λ-calculus template the name is in scope within the entire body but is out of scope outside of the lambda expression { label name ( lambda arguments. body_references_name } in Lisp can use labels to define a mutually recursive set of functions ( labels (list of named lambda expressions sequence of forms using the temporarily named functions CSE-3401-May example of recursion a recursive multiply that uses only addition the temporary function is called mult use quote and eval not function (eval '(labels ((mult (k n (cond ((zerop n 0 (t (+ k (mult k (1- n (mult 2 3 CSE-3401-May

9 example of recursion (cont d rectimes computes k * n by supplying the paramters to a unary function that is a variation of previous example (defun rectimes (k n (labels (( temp (n (cond ((zerop n 0 ( t (+ k (temp (1- n (temp n CSE-3401-May Functional Programming see notes on functional programming Shakil M. Khan adapted from Gunnar Gotshalks

10 functional programming: history 1977 Turing 1 Lecture John Backus described functional programming the problem with current languages is that they are word-at-a-time 2 notable exceptions then were Lisp and APL now ML, Haskell and others 1 Turing award is the Nobel prize of computer science 2 Word-at-a-time translates to byte-at-a-time in modern jargon. A word typically held 2 to 8 bytes depending upon the type of computer CSE-3401-May meaningful units of work work with operations meaningful to the application, not to the underlying hardware & software analogy with word processing is not to work with characters and arrays or lists of characters but work with words, paragraphs, sections, chapters and even books at a time, as appropriate CSE-3401-May

11 requires abstraction abstract out the control flow patterns give them names to easily reuse the control pattern for example in most languages we explicitly write a loop every time we want to process an array of data if we abstract out the control pattern, we can think of processing the entire array as a single operation CSE-3401-May example 1 consider the inner product of two vectors < a1, a2,..., an > < b1, b2,..., bn > ( a1*b1 + a2*b an*bn in Java or C/C++, the following is an algorithm result = 0; for (i = 1, i <= n, i++ { result = result + a[i]*b[i]; } note the explicit loop (or recursion and introduction of variables result, i and n (have to explicitly know the length of the vectors CSE-3401-May

12 example 1 FP form innerproduct ::= (/ + o (α x o trans note the following properties of functional programs NO explicit loops ( or recursion NO sequencing at a low level NO local variables in addition, functional programs have the following properties functions as input in the above + (plus, x (times, trans functions as output not shown in the above in FP frequently write functions that produce a new function using other functions as input CSE-3401-May evaluating (/ + o (α x o trans apply the function to a single argument consisting of a list of the actual arguments. innerproduct : < < a1,..., an > < b1,... bn > > work from right to left o is function composition f o g : x f ( g ( x thus we execute trans first which means the transpose of a matrix swap rows and columns trans : < < a1,..., an > < b1,... bn > > < < a1, b1> < a2, b2 >... < an, bn > > CSE-3401-May

13 evaluating (cont d now execute (α (α read asapply times to all means apply the function (times to all items in the argument list (α : < < a1, b1> < a2, b2 >... < an, bn > > < a1 b1, a2 b2,..., an bn > now execute (/ + (/ + read asreduce using + means put the function + (plus between the arguments and apply from left to right (/ + : < a1 b1, a2 b2,..., an bn > a1 b1 + a2 b an bn and we have the inner product CSE-3401-May Backus notation (BN and Lisp data structures the list Lisp ( a b c d BN < a, b, c, d > the list is a fundamental structure we will see it again in Prolog selector functions Lisp car / first, cdr / rest BN tail (equivalent to rest, 1, 2, 3,... as needed or implemented, select item from the list constructor functions Lisp cons BN [ f-1, f-2,..., f-n ] eachf-i operates on the input to produce a list as output CSE-3401-May

14 BN and Lisp (cont d choice if then else Lisp ( cond ( p.1 s.1-1 s s.1-p ( p.2 s.2-1 s s.2-q... ( p.n s.n-1 s.n-2... s.n-r BN p.1 --> function.1 ; p.2 --> function.2 ; ; p.n --> function.n if p.1 then function.1 else CSE-3401-May BN and Lisp (cont d function application Lisp ( f x1... xn, ( apply f (x1... xn, ( funcall f x1... xn BN f : < x1,... xn > mapping functions Lisp ( mapcar f..., ( maplist f...,... BN (α f other functions reduction composition binding constant Lisp ( reduce f x ( comp f g ( bu f k literal BN ( / f f o g ( bu f k literal CSE-3401-May

15 inner product one argument versions lisp recursive version (defun innerproduct (a-b-pair ( cond (( null (car a-b-pair 0 ( t ( + ( * (caar a-b-pair (caadr a-b-pair (innerproduct (list (cdar a-b-pair (cdadr a-b-pair CSE-3401-May inner product one argument versions (cont d lisp functional version ( defun innerproduct ( a-b-pair ( reduce + ( mapcar * ( first a-b-pair ( second a-b-pair mapcar does transpose due to having multiple arguments Backus notation innerproduct ::= ( / + o ( α x o trans CSE-3401-May

16 library of functions depending upon the application area other functions are created. e.g. trans transpose a matrix some are created using existing functionals e.g. innerproduct CSE-3401-May library of functions (cont d others are created outside of the system for efficiency reasons e.g. trans may be more efficient to implement outside of Lisp although as compiler knowledge grows compilers produce more efficient code than coding by hand machine speeds increase so many functions execute fast enough the file contains additional library functions CSE-3401-May

17 binding function bu given a binary function it is often useful to bind the first parameter to a constant creating a unary function also called currying after the mathematician Curry who developed the idea (bu + 3 creates a unary add 3 from the binary function + (mapcar (bu + 3 (1 2 3 (4 5 6 cons x before every item in a list (mapcar (bu cons x (1 2 3 ((x.1 (x.2 (x.3 note that mapcar expects a function definition as the second argument, so we use bu to help construct the function CSE-3401-May bu (cont d we could define the function 3+ ( define 3+ ( x ( + 3 x and use (mapcar 3+ (1 2 3 (4 5 6 but this adds to our name space for use-once functions we can use lambda expressions (mapcar # (lambda (x (+ 3 x (1 2 3 (4 5 6 (mapcar (function( lambda (x (+ 3 x (1 2 3 (4 5 6 CSE-3401-May

18 bu (cont d the previous slide solutions are seen as being clumsy and more difficult to read compared to the following bu has a clear meaning with the above you have to reverse engineer to understand (mapcar (bu + 3 (1 2 3 ==> (4 5 6 can define functions using bu (defun 3+ (y (funcall (bu + 3 y in such cases we would rather write (defun 3+ (y (+ 3 y we do not normally use bu to define named functions CSE-3401-May bu (cont d BU is defined as follows (defun bu (f x # (lambda (y (funcall f x y the long form (defun bu (f x (function (lambda (y (funcall f x y BU uses a function as input and produces a function as output CSE-3401-May

19 bu (cont d how does Lisp represent the output of bu? in GCL (Gnu Common Lisp you can see what takes place (bu + 3 (LAMBDA-CLOSURE ( ( X 3 ( F + ( ( (BU BLOCK (Y (FUNCALL F X Y we see the parameter and body from the definition of bu together with the bindings ((X 3 (F + the closure adds the bindings to the environment so the body uses those bindings when it executes CSE-3401-May the functional rev rev reverse the order of the arguments of a binary function (defun rev (f # (lambda (x y(funcall f y x earlier we wrote (mapcar (bu cons a (1 2 3 ((a.1 (a.2 (a.3 suppose we want ((1.a (2.a (3.a then we write (mapcar (bu (rev cons a (1 2 3 ((1.a (2.a (3.a CSE-3401-May

20 other useful functionals on course page (functionals.lsp and the notes on functionals, the following functionals are described (comp unaryfunction1 unaryfunction2 compose two unary functions (compl unaryfunction1 unaryfunction2... unaryfunctionn compose a list of unary functions (trans matrix see slides on developing functional programs CSE-3401-May other useful functionals (cont d (distl anitem thelist distribute anitem to the left of items in thelist (distl a (1 2 3 ((a 1 (a 2 (a 3 (distr anitem thelist distribute anitem to the right of items in thelist (distr a (1 2 3 ((1 a (2 a (3 a CSE-3401-May

21 Examples of how a Functional Program can be Developed from an existing recursive program analysis of input and output diagrams Shakil M. Khan adapted from Gunnar Gotshalks transpose a 2d matrix 1 2-d matrix is represented as a list of rows all of the same length e.g ( ( ( ( the transpose (swap rows and columns of the above is ( ( ( ( CSE-3401-May

22 transpose a 2d matrix 2 (defun trans ( thematrix (cond ( ( null ( car thematrix nil ( t ( cons ( firstofeach thematrix ( trans (restofeach thematrix (defun firstofeach ( thematrix ; extract first of each row (cond ( ( null thematrix nil ( t (cons ( caar thematrix ( firstofeach ( cdr thematrix (defun restofeach ( thematrix ; remove first of each row (cond ( ( null thematrix nil ( t ( cons ( cdar thematrix ( restofeach ( cdr thematrix CSE-3401-May transpose a 2d matrix 3 analysis of the transpose program shows that trans invokes firstofeach to every decreasing rows (restofeach this is what maplist does so a first pass of trans becomes (defun trans (thematrix (maplist firstofeach thematrix (trans ( (1 2 3 (4 5 6 (7 8 9 ((1 4 7 (4 7 (7 what went wrong? CSE-3401-May

23 transpose a 2d matrix 4 put a print statement in firstofeach (defun firstofeach (thematrix ; extract first of each row (print thematrix (cond ((null thematrix nil (t (cons (caar thematrix (firstofeach (cdr thematrix CSE-3401-May transpose a 2d matrix 5 output ((1 2 3 (4 5 6 (7 8 9 ; first call from maplist ((4 5 6 (7 8 9 ; recursion ((7 8 9 NIL ((4 5 6 (7 8 9 ; second call from maplist ((7 8 9 ; recursion NIL ((7 8 9 ; third call from maplist NIL ; recursion ((1 4 7 (4 7 (7 ; the answer CSE-3401-May

24 transpose a 2d matrix 6 maplist is removing the rows not the first of each row because maplist is working on the matrix a row at a time input is ( (1 2 3 (4 5 6 ( one list of rows we want maplist to work on each row input should be (1 2 3 (4 5 6 ( three lists this is a common problem we want to remove the outer parenthesis recall that apply removes the outer level of parenthesis when invoking a function on arguments thus trans becomes (defun trans (thematrix (apply maplist firstofeach thematrix CSE-3401-May transpose a 2d matrix 7 we try trans and get an error message such as Error: Expected 1 args but received 3 args Fast links are on: do (si::use-fast-links nil for debugging Error signalled by MAPLIST Broken at FIRSTOFEACH CSE-3401-May

25 transpose a 2d matrix 8 ah! now we have one argument for each row as input to firstofeach, but it expects a single argument a list of rows use the keyword &rest to collect all the arguments into one (defun firstofeach ( &rest thematrix (print thematrix ( cond ( ( null thematrix nil ( t ( cons ( caar thematrix ( firstofeach ( cdr thematrix CSE-3401-May transpose a 2d matrix 9 we try trans and get infinite recursion the print statement shows the following for the first few lines ((1 2 3 (4 5 6 (7 8 9 (((4 5 6 (7 8 9 ; list nested one deeper (NIL (NIL (NIL goes on forever CSE-3401-May

26 transpose a 2d matrix 10 each recursive call to firstofeach adds a layer of parenthesis again a common error we need to remove the parenthesis before the recursive call use apply (defun firstofeach ( &rest thematrix ( cond ( ( null thematrix nil ( t ( cons ( caar thematrix ( apply 'firstofeach ( cdr thematrix CSE-3401-May transpose a 2d matrix 11 trans now works with the upper level being a functional, but firstofeach is still recursive (defun trans ( thematrix ( apply maplist firstofeach thematrix (defun firstofeach ( &rest thematrix ( cond ( ( null thematrix nil ( t ( cons ( caar thematrix ( apply firstofeach ( cdr thematrix CSE-3401-May

27 transpose a 2d matrix 12 notice that firstofeach takes the first item from each sublist (defun firstofeach (&rest thematrix (cond ((null thematrix nil (t (cons (caar thematrix (apply firstofeach (cdr thematrix car gives the first of a list and mapcar will apply it to every sublist in a list and collect the results in a list so we have (defun firstofeach ( &rest thematrix ( mapcar car thematrix CSE-3401-May transpose a 2d matrix 13 we now have two functionals for the solution (defun trans ( thematrix ( apply maplist firstofeach thematrix (defun firstofeach ( &rest thematrix ( mapcar car thematrix using lambda we can eliminate firstofeach (defun trans ( thematrix (apply maplist # ( lambda ( &rest thematrix ( mapcar car thematrix thematrix CSE-3401-May

28 transpose a 2d matrix 14 but nothing beats creative insight and knowledge of available operations!!! the following gives the transpose (defun trans ( thematrix (apply 'mapcar 'list thematrix CSE-3401-May all pairs functional 1 we want the following functional allpairs : < <a, b, c> <1, 2, 3, 4> > input < <a,1> <a,2> <a,3> <a,4> <b,1> <b,2> <b,3> <b,4> output <c,1> <c,2> <c,3> <c,4> > we make use of the picture of the input and output to infer a functional solution CSE-3401-May

29 all pairs functional 2 looking at the functionals in the library it seems that distribution may be useful lets try it distl : < <a, b, c> <1, 2, 3, 4> > < << a, b, c >, 1 > << a, b, c >, 2 > << a, b, c >, 3 >... > looks good but we want to distribute second argument over the first rev could be used but we have distr distr : < <a, b, c> <1, 2, 3, 4> > < < 1, < a, b, c > > < 2, < a, b, c > > < 3, < a, b, c >... > CSE-3401-May all pairs functional 3 we have distr : < <a, b, c> <1, 2, 3, 4> > < < 1, < a, b, c > > < 2, < a, b, c > > < 3, < a, b, c >... > if we distribute right the numbers over each list we have < < <a, 1>, < b, 1>, <c, 1 > >... > but examining the output we see that a is repeated first not the 1 < <a,1> <a,2> <a,3> <a,4> <b,1> <b,2> <b,3> <b,4> output <c,1> <c,2> <c,3> <c,4> > CSE-3401-May

30 all pairs functional 4 what we need to do is to reverse the order of the arguments so the letters are distributed first distr o [ 2, 1] : < <a, b, c> <1, 2, 3, 4> > distr : < <1, 2, 3, 4> <a, b, c> > < < a, < 1, 2, 3, 4 > > < b, < 1, 2, 3, 4 > >... > now if we apply distribute left to each sublist we have (α distl : < < a, < 1, 2, 3, 4 > > < b, < 1, 2, 3, 4 > >... > < < < a, 1 > < a, 2 > < a, 3 > < a, 4 > > < < b, 1 >... > CSE-3401-May all pairs functional 5 so far we have (α distl o distr o [ 2, 1] < < < a, 1 > < a, 2 > < a, 3 > < a, 4 > > < < b, 1 >... > but we have the pairs nested within an extra pair of lists what we need to do is to reduce the lists into one using append (/ append : < < < a, 1 > < a, 2 > < a, 3 > < a, 4 > > < < b, 1 >... > < < a, 1 > < a, 2 > < a, 3 > < a, 4 > < b, 1 >... > CSE-3401-May

31 all pairs functional 6 so the final function definition is allpairs ::= (/ append o (α distl o distr o [ 2, 1] other orderings are possible using other combinations of swapping or not swapping the initial lists and using left or right distribution for the second distribution allpairs ::= (/ append o (α distr o distr o [ 2, 1] allpairs ::= (/ append o (α distl o distr allpairs ::= (/ append o (α distr o distr CSE-3401-May info assignment/report 1 is out due June 4 hand in hard copy + submit soft copy (programs only: submit 3401 r1 <files> class test 1 (7:00~8:30 on June??? June 4 (2 weeks from now June 11 (3 weeks from now included everything covered up to June??? CSE-3401-May

### Lambda Calculus. Gunnar Gotshalks LC-1

Lambda Calculus LC-1 l- Calculus History Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of l-calculus is equivalent in power to

### History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

History Functional Programming Based on Prof. Gotshalks notes on functionals 1977 Turing 1 Lecture John Backus described functional programming The problem with current languages is that they are word-at-a-time

### Functional Programming. Contents

Functional Programming Gunnar Gotshalks 2003 September 27 Contents Introduction... 2 Example for Inner Product... 2 Basis Set for Functionals... 3 Example Function Definitions... 3 Lisp Builtin Functionals...

### Lambda Calculus LC-1

Lambda Calculus LC-1 λ- Calculus History-1 Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of λ-calculus is equivalent in power

### Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

λ Calculus Basis Lambda Calculus and Lambda notation in Lisp II Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky Mathematical theory for anonymous functions» functions that have

### Functional Programming. Big Picture. Design of Programming Languages

Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

### Functional Programming

Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

### Lisp Basic Example Test Questions

2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2

### Functional programming with Common Lisp

Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

### FP Foundations, Scheme

FP Foundations, Scheme In Text: Chapter 15 1 Functional Programming -- Prelude We have been discussing imperative languages C/C++, Java, Fortran, Pascal etc. are imperative languages Imperative languages

### COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

### Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

### CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

### Functional Languages. Hwansoo Han

Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

### Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

### Project 2: Scheme Interpreter

Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

### 11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functional programming FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the

### Introduction to Scheme

How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

### LECTURE 16. Functional Programming

LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

### Introduction. chapter Functions

chapter 1 Introduction In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main

### Example Scheme Function: equal

ICOM 4036 Programming Languages Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: LISP Introduction to

### User-defined Functions. Conditional Expressions in Scheme

User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

### Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

### CSCE 314 Programming Languages

CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee 1 Contents 1. Historical Background of Haskell 2. Lazy, Pure, and Functional Language 3. Using ghc and ghci 4. Functions 5. Haskell Scripts

### Functional Programming Languages (FPL)

Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

### 4/19/2018. Chapter 11 :: Functional Languages

Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken by Alan Turing, Alonzo Church, Stephen

### Programming Languages

Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages

### 1.3. Conditional expressions To express case distinctions like

Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

### Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology

### Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

### Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Functional Programming

Functional Programming CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario When the Function is the Thing In O-O programming, you typically know where an action is needed,

### 6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

### CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Basics 2 Contents 1. Jump into Haskell: Using ghc and ghci (more detail) 2. Historical Background of Haskell 3. Lazy, Pure, and Functional

### Midterm Examination (Sample Solutions), Cmput 325

Midterm Examination (Sample Solutions, Cmput 325 [15 marks] Consider the Lisp definitions below (defun test (L S (if (null L S (let ((New (add (cdar L S (test (cdr L New (defun add (A S (if (null S (cons

### CS 6110 S14 Lecture 1 Introduction 24 January 2014

CS 6110 S14 Lecture 1 Introduction 24 January 2014 1 Introduction What is a program? Is it just something that tells the computer what to do? Yes, but there is much more to it than that. The basic expressions

### UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

### Chapter 11 :: Functional Languages

Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Copyright 2016 Elsevier 1 Chapter11_Functional_Languages_4e - Tue November 21, 2017 Historical Origins The imperative

### Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I by John McCarthy Ik-Soon Kim Winter School 2005 Feb 18, 2005 Overview Interesting paper with By John Mitchell Interesting

### ALISP interpreter in Awk

ALISP interpreter in Awk Roger Rohrbach 1592 Union St., #94 San Francisco, CA 94123 January 3, 1989 ABSTRACT This note describes a simple interpreter for the LISP programming language, written in awk.

### CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

COMPUTER SCIENCE 320 CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination FRIDAY, MARCH 3, 2006 Problem 1. [25 pts.] A special form is an expression that is not evaluated according to the

### Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology

### A Small Interpreted Language

A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

### FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší

### Functional Programming

Functional Programming Björn B. Brandenburg The University of North Carolina at Chapel Hill Based in part on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts. Brief

### Chapter 1. Fundamentals of Higher Order Programming

Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

### Notes on Higher Order Programming in Scheme. by Alexander Stepanov

by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

### Introductory Example

CSc 520 Principles of Programming Languages 21: Lambda Calculus Introduction Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg

### CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

### Imperative, OO and Functional Languages A C program is

Imperative, OO and Functional Languages A C program is a web of assignment statements, interconnected by control constructs which describe the time sequence in which they are to be executed. In Java programming,

### SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018 Class Information Midterm exam forum open in Sakai. HW4 and

### Programming Language Pragmatics

Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken Alan Turing, Alonzo Church, Stephen

### Programming Systems in Artificial Intelligence Functional Programming

Click to add Text Programming Systems in Artificial Intelligence Functional Programming Siegfried Nijssen 8/03/16 Discover thediscover world at the Leiden world University at Leiden University Overview

### An Explicit-Continuation Metacircular Evaluator

Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information

### Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Racket Next 2+ weeks will use the Racket language (not ML) and the DrRacket programming environment (not emacs) Installation / basic usage instructions on course website CSE34: Programming Languages Lecture

### University of Massachusetts Lowell

University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

### (Refer Slide Time: 4:00)

Principles of Programming Languages Dr. S. Arun Kumar Department of Computer Science & Engineering Indian Institute of Technology, Delhi Lecture - 38 Meanings Let us look at abstracts namely functional

### Functional Programming Lecture 1: Introduction

Functional Programming Lecture 1: Introduction Viliam Lisý Artificial Intelligence Center Department of Computer Science FEE, Czech Technical University in Prague viliam.lisy@fel.cvut.cz Acknowledgements

### COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

### CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught

### Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Introduction to LISP York University Department of Computer Science and Engineering York University- CSE 3401- V. Movahedi 11_LISP 1 Introduction to LISP Evaluation and arguments S- expressions Lists Numbers

### 6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

### Putting the fun in functional programming

CM20167 Topic 4: Map, Lambda, Filter Guy McCusker 1W2.1 Outline 1 Introduction to higher-order functions 2 Map 3 Lambda 4 Filter Guy McCusker (1W2.1 CM20167 Topic 4 2 / 42 Putting the fun in functional

### Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

### Chapter 15. Functional Programming Languages ISBN

Chapter 15 Functional Programming Languages ISBN 0-321-49362-1 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language:

### This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

1 2 This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator (total) and a counter (i); it works by assigning

### 15 Unification and Embedded Languages in Lisp

15 Unification and Embedded Languages in Lisp Chapter Objectives Chapter Contents Pattern matching in Lisp: Database examples Full unification as required for Predicate Calculus problem solving Needed

### Imperative languages

Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

### Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Functional Programming and λ Calculus Amey Karkare Dept of CSE, IIT Kanpur 0 Software Development Challenges Growing size and complexity of modern computer programs Complicated architectures Massively

### Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters Corky Cartwright January 26, 2018 Denotational Semantics The primary alternative to syntactic semantics is denotational semantics.

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases

### SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### CS 242. Fundamentals. Reading: See last slide

CS 242 Fundamentals Reading: See last slide Syntax and Semantics of Programs Syntax The symbols used to write a program Semantics The actions that occur when a program is executed Programming language

### Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

### Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

### Robot Programming with Lisp

4. Functional Programming: Higher-order Functions, Map/Reduce, Lexical Scope Institute for Artificial University of Bremen 9 of November, 2017 Functional Programming Pure functional programming concepts

### CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018 Typical workflow concrete syntax (string) "(fn x => x + x) 4" Parsing Possible errors / warnings

### 5. Introduction to the Lambda Calculus. Oscar Nierstrasz

5. Introduction to the Lambda Calculus Oscar Nierstrasz Roadmap > What is Computability? Church s Thesis > Lambda Calculus operational semantics > The Church-Rosser Property > Modelling basic programming

### INF4820. Common Lisp: Closures and Macros

INF4820 Common Lisp: Closures and Macros Erik Velldal University of Oslo Oct. 19, 2010 Erik Velldal INF4820 1 / 22 Topics for Today More Common Lisp A quick reminder: Scope, binding and shadowing Closures

### Functional programming in LISP

Programming Languages Week 4 Functional programming in LISP College of Information Science and Engineering Ritsumeikan University review of part 3 enumeration of dictionaries you receive a sequence of

### CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

### Introduction to the Lambda Calculus

Introduction to the Lambda Calculus Overview: What is Computability? Church s Thesis The Lambda Calculus Scope and lexical address The Church-Rosser Property Recursion References: Daniel P. Friedman et

### An introduction to Scheme

An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

### CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

CS152: Programming Languages Lecture 7 Lambda Calculus Dan Grossman Spring 2011 Where we are Done: Syntax, semantics, and equivalence For a language with little more than loops and global variables Now:

### Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also

### Lists. Michael P. Fourman. February 2, 2010

Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

### Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Whereweare CS-XXX: Graduate Programming Languages Lecture 7 Lambda Calculus Done: Syntax, semantics, and equivalence For a language with little more than loops and global variables Now: Didn t IMP leave

### 11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Outline FP Foundations, Scheme In Text: Chapter 15 Mathematical foundations Functional programming λ-calculus LISP Scheme 2 Imperative Languages We have been discussing imperative languages C/C++, Java,

### Chapter 15 Functional Programming Languages

Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions.

### Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Fundamentals and lambda calculus Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Logistics Assignments: Programming assignment 1 is out Homework 1 will be released tomorrow night Podcasting:

### SOFTWARE ARCHITECTURE 6. LISP

1 SOFTWARE ARCHITECTURE 6. LISP Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ 2 Compiler vs Interpreter Compiler Translate programs into machine languages Compilers are