Week 4: Functional Programming

Size: px
Start display at page:

Download "Week 4: Functional Programming"

Transcription

1 CS320 Principles of Programming Languages Week 4: Functional Programming Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Week 4: Functional Programming 1/ 66

2 Topic List for this Unit Functional programming an important programming paradigm Lambda calculus theoretical foundation Lisp/Scheme language design and programming style ML/Haskell abstract data types (later) PSU CS320 Fall 17 Week 4: Functional Programming 2/ 66

3 Functional Programming Functional programming is one of the main programming paradigms: Imperative programming Object-oriented programming Functional programming Logic programming Functional programming uses the construction and application of functions as the basic mode of computation. Functional programming s theoretical foundation is not Turing machine; it is lambda calculus. PSU CS320 Fall 17 Week 4: Functional Programming 3/ 66

4 Functional Programming Advantages Higher Programming Level: Programs written in a functional language typically contain less details about implementation. A functional program describes what to compute instead of how to compute. Easier to Reason and Transform Programs: Features supported by functional languages (e.g. referential transparency) make it easier to analyze and transform a program. For example, macro-expanding a function call is always legal. PSU CS320 Fall 17 Week 4: Functional Programming 4/ 66

5 Basic Concepts Side effect Referential transparency Higher-order functions First-class functions Pure vs. impure functional languages PSU CS320 Fall 17 Week 4: Functional Programming 5/ 66

6 Side Effect A function or expression is said to have a side effect if, in addition to producing a value, it also modifies some state. Example: Side-effecting expressions are common in imperative languages: ++i, (a = 3) > b, y = x = 6 // C also exist in some functional languages: (read), (set! x (+ x 1)) // Scheme (print(x); print("\n")) // ML PSU CS320 Fall 17 Week 4: Functional Programming 6/ 66

7 Side Effect Functions in an imperative language can easily have side effects: void sum(int *s, int a) { *s += a; } int counter() { static c = 0; return ++c; } int safe_divide(int x, int y) { if (y == 0) { printf("error: divisor is 0\n"); return 0; } else return x / y; } Function does not return value, yet content pointed to by s is changed. Function changes program state through a static variable. If y==0, functionchangesprogram state through printf(). PSU CS320 Fall 17 Week 4: Functional Programming 7/ 66

8 Side Effect However, functions in an imperative language can also be made side-effect free if, they use only non-pointer, call-by-value parameters they reference no non-local variables they have no side-effecting statements (e.g. I/O statements) int average(int a, int b) { return (a + b) / 2; } PSU CS320 Fall 17 Week 4: Functional Programming 8/ 66

9 Referential Transparency A function or expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program. Absence of side effects is a necessary, but not sufficient, condition for referential transparency. Question: Why? Answer: Consider a function that returns the value of a global or environment variable, such as, gettimeofday(). It reads but does not modify the variable, hence it does not have side effects. Yet, it is not referentially transparent. Different calls to the function may return different values. PSU CS320 Fall 17 Week 4: Functional Programming 9/ 66

10 Referential Transparency Implications: If a function is referentially transparent then its value depends only on its arguments, and it has no side effect. Such a function is called a pure function. Areferentiallytransparentfunctionwithnoparametersmust always return the same value. Thus it is no different than a constant. Examples: Pure math functions, such as gcd(), are referentially transparent. Clibraryfunctionrand() is not because it depends on the state of the machine and previous calls to itself. PSU CS320 Fall 17 Week 4: Functional Programming 10 / 66

11 Referential Transparency Benefits: Program transformations f(x)+f(x) is always equivalent to 2*f(x). Reasoning about program behavior The result of a function, f(x), can be derived from its input through a sequence of program transformations. This is called equational reasoning. Parallel evaluation All arguments of f(e 1,e 2,...,e k ) can be evaluated in parallel. PSU CS320 Fall 17 Week 4: Functional Programming 11 / 66

12 Equational Reasoning Similar to using arithmetic properties (such as communitivity and associativity) to simply algebraic expressions However, in addition to built-in operators properties, user-defined functions can also be used as properties for reasoning Example: Consider the reverse function definition in Haskell: reverse :: [a] -> [a] -- type signature reverse [] = [] -- def for empty list case reverse (x:xs) = reverse xs ++ [x] -- def for all other cases We want to show that reverse has no effect on singleton lists, i.e. reverse [x] = [x] for any value x. Reasoning: reverse [x] = reverse (x:[]) -- applying reverse (general case) = reverse [] ++ [x] -- applying reverse (empty list) = [] ++ [x] -- applying ++ = [x] PSU CS320 Fall 17 Week 4: Functional Programming 12 / 66

13 Higher-Order Functions First-order functions arguments and return value are non-functions,, f Higher-order functions restrictions removed,, f,, f,, f Higher-order function means a function that either takes a function as an argument or returns a function as a result (or both). PSU CS320 Fall 17 Week 4: Functional Programming 13 / 66

14 First-Class Functions Higher-order-ness is a property of an individual function, while first-class-ness is a property of a programming language. First-class functions mean that functions are being treated as (first-class) values. Specifically, first-class functions mean that: Functions can be passed as arguments to other functions Functions can be returned as results from other functions Functions can be declared in any scope (parallel to variable declarations) Functions can be defined without a name (parallel to literals of other types) PSU CS320 Fall 17 Week 4: Functional Programming 14 / 66

15 First-Class Functions Imperative languages may support a subset of the first-class features, for example, Pascal allows functions to be defined in nested scopes C allows functions to be passed as arguments and returned as return values But, none of them supports the full set of features. Reason: Need to use heap storage for handling function calls. PSU CS320 Fall 17 Week 4: Functional Programming 15 / 66

16 Functional Languages Languages that support functional programming style are called functional programming languages. All functional languages treat functions as first-class values. Pure functional languages They further support referential transparency for all functions and expressions (hence no side effect at all). (Example: Haskell) Impure functional languages They allow side effects, although their major mode of computation is still functional. (Example: Scheme) Note that there are also programming languages that have no side effects, but do not support first-class functions. They are not functional languages; they are called single-assignment languages. (Example: SISAL) PSU CS320 Fall 17 Week 4: Functional Programming 16 / 66

17 Functional Languages Functional languages may look and feel quite differently from imperative languages: They typically don t have many of the features that imperative languages deem essential: mutable variables and assignments mutable data structures such as arrays nested scopes through statement blocks loop constructs Instead, they have immutable variables and definitions immutable lists nested scopes through nested expressions first-class functions recursion lazy evaluation PSU CS320 Fall 17 Week 4: Functional Programming 17 / 66

18 Immutable Variables Some functional languages (e.g. ML) have assignment-like statements. How do they differ from assignment statements in imperative languages? Compare C++ int x = 5; int &r = x; x = 6; ML val x = 5; val r = ref x; val x = 6; In C++, after the assignment, x s value will be 6, and the reference variable r will point to 6 as well. In ML, changing a variable s value means producing a new copy of the variable, but leaves the old one alone. After the assignment, x s value will be 6, but r still references the old copy of x, i.e.!r = 5. PSU CS320 Fall 17 Week 4: Functional Programming 18 / 66

19 Immutable Lists What about updating components of a (large) aggregate object? In the following code, what actually happens when an insert is performed? ML fun insert x nil = [x] insert x (h::t) = if h<x then h::(insert x t) else x::h::t; val L = [1,2,4,5,6,7,8,9]; val L = insert 3 L; We get a new list, containing a new element, and a new copy of every other node (even though the values of these nodes haven t changed). Old list still exists. (It should eventually be garbage collected if no longer accessible.) PSU CS320 Fall 17 Week 4: Functional Programming 19 / 66

20 Nested Scopes Consider a nest of scopes in C, created by statement blocks: int z; { int x = 3, y = 5; { int x = x * 2; z = x + y; } } Question: How can achieve the same effect in a function program? Answer: With nested let constructs, which are expressions. var z = let var x = 3; var y = 5 in let var x = x * 2 in x + y end end; PSU CS320 Fall 17 Week 4: Functional Programming 20 / 66

21 HigherOrder Function Applications Currying A multi-argument function takes its arguments one at a time. fun mult a b = a * b fun triple = mult 3 The expression mult 3 represents a function, which when applied to a number, triples the value of that number, i.e. triple b mult 3 b Composition Iff : X Y and g : Y Z, then the composition of g and f, g f : X Z, is given by (g o f)(x) = g(f(x)) PSU CS320 Fall 17 Week 4: Functional Programming 21 / 66

22 Higher-Order Function Applications Map Applies some function to each member of a list and returns the resulting list. map triple [1, 2, 3, 4] = [triple 1, triple 2, triple 3, triple 4] = [3, 6, 9, 12] Fold Computes a value working from the tail of a list to the head (from right to left); at each step, applies the given function to each element and the previously computed result. foldr (+) 0 [2,4,7] = (2 + (4 + (7 + 0))) = 13 sumlist = foldr + 0 multlist = foldr * 1 sumlist [1,2,3,4] = 10 multlist [1,2,3,4] = 24 PSU CS320 Fall 17 Week 4: Functional Programming 22 / 66

23 Recursion In functional languages, recursions replace loops. Consider the factorial function: fac(n) =if(n = 0) then 1 else n fac(n 1) Scheme (define (fac n) (if (= n 0) 1 (* n (fac (- n 1))))) ML fun fac n = if n=0 then 1 else n * fac(n-1); However, a recursive function in general is more expensive to implement than a loop, because it needs the support of a stack. Compilers for functional languages try very hard to convert recursive functions to better forms, such as the tail-recursive form. PSU CS320 Fall 17 Week 4: Functional Programming 23 / 66

24 Tail Recursion Tail recursion is a special form of recursion, in which the only recursive call is the last act of the recursive function: // Factorial function, // not tail-recursive int fac(int n) { if (n <= 1) return 1; return n * fac(n-1); } // Tail-recursive, but // not a meaningful function! int g(int n) { if (n <= 1) return 1; return g(n-1); } Property: Any tail-recursive function can be converted to a non-recursive function with an iteration construct; hence can be implemented efficiently. Question: Can we convert the recursive factorial function into a tailrecursive one? The answer is Yes. PSU CS320 Fall 17 Week 4: Functional Programming 24 / 66

25 Tail Recursion Let s trace the recursive factorial function; at each step, record the cumulative partial result. Step Recursive call Partial result 0 fac(n) 1 1 fac(n-1) n 2 fac(n-2) n*(n-1) 3 fac(n-3) n*(n-1)*(n-2) n-1 fac(1) n*(n-1)* *2 n n! Now let s define a helper function with two parameters, one tracks the recursive call s argument, the other the partial result: helper(k, res) =if(k 1) then helper(k 1, res k) else res Function helper is tail-recursive, and function fac can be defined as: fac(n) =helper(n, 1) PSU CS320 Fall 17 Week 4: Functional Programming 25 / 66

26 Tail Recursion Here is a C version of the tail-recursive factorial program: int helper(int k, int res) { if (k >= 1) return helper(k-1, res*k); return res; } int fac(int n) { return helper(n, 1); } Amatchingnon-recursive program: int helper(int k, int res) { while (k >= 1) { res = res * k; k--; } return res; } int fac(int n) { return helper(n, 1); } PSU CS320 Fall 17 Week 4: Functional Programming 26 / 66

27 Tail Recursion Now an ML version: fun fac n = let fun helper(k, res) = if k >= 1 then helper(k-1, res*k) else res in helper(n, 1) end; PSU CS320 Fall 17 Week 4: Functional Programming 27 / 66

28 Lazy Evaluation A feature that appears in some functional languages. It concerns with how a function s arguments are evaluated. Example: mult(fac(3), fac(4)) Normal (Eager) Evaluation Evaluate arguments before invoking a function call (Think call by value ) 1. eval fac(3) 6; eval fac(4) eval mult(6,24) 144 Lazy Evaluation Substitute arguments into a function s body (Think call by name ) 1. reduce mult fac(3) fac(4) 2. reduce fac PSU CS320 Fall 17 Week 4: Functional Programming 28 / 66

29 Lazy Evaluation Application Lazy evaluation is useful for handling unbounded lists: [1..] -- defines an unbounded list, but only the -- first element is explicitly named; the -- list generator can produce any other -- element when needed head [1.. ] -- => 1 head (tail [1.. ]) -- => 2 head (map triple [1.. ]) -- => 3 -- the computation is demand-driven, only -- enough computation is performed to -- produce the result PSU CS320 Fall 17 Week 4: Functional Programming 29 / 66

30 Lambda Calculus An abstract notation for defining computable functions developed in the 1930s by Lorenzo Church. It is a Turing-complete, universal model of computation It is the foundation for Lisp and other functional languages It is also used in theoretical studies of programming languages Lambda calculus consists of a notation for function expressions (λ-expressions) and a number of calculation rules (reductions). PSU CS320 Fall 17 Week 4: Functional Programming 30 / 66

31 Lambda Expressions A λ-expression can be in one of three forms: M x λx.m MM x λx.m M 1 M 2 Examples: variable, x is a variable name abstraction, denotes a single-argument function application, it s the application of function M 1 to argument M 2 λx.x (λx.x)y λf.λg.λx.f (gx) PSU CS320 Fall 17 Week 4: Functional Programming 31 / 66

32 Common Conventions Variables are typically represented by lower-case letters; sub-expressions are typically represented by upper-case letters The body of an abstraction extends as far right as possible: λx.mn means λx.(mn) and not (λx.m)n Applications are left associative: XYZ means (XY )Z Nested lambdas may be collapsed together: λx.λy.xyx can be written as λxy.xyx White space is insignificant: XY and XY are exactly the same PSU CS320 Fall 17 Week 4: Functional Programming 32 / 66

33 Functions of Multiple Arguments A function f (x, y) of two arguments may be represented by a function λx.(λy.m) which is of a single argument that, when applied, returns a second function that accepts a second argument and then computes a result in the same way as f. Using the common convention, the above lambda expression can be simplified: λx.(λy.m) =λx.λy.m = λxy.m This idea of defining functions with multiple arguments is called currying, and is widely used in functional languages. PSU CS320 Fall 17 Week 4: Functional Programming 33 / 66

34 Values and Operators Pure lambda calculus has only variables and functions, nothing else. However, values, operators, and other constructs can be defined by λ-expressions. Church encoding for integers Define each integer as a function of two parameters, f and x. The integer s value is encoded as the number of times f is applied to x. Number Function Definition Lambda Expression 0 0(f, x) =x 0=λfx.x 1 1(f, x) =f (x) 1=λfx.fx 2 2(f, x) =f (f (x)) 2=λfx.f (fx) 3 3(f, x) =f (f (f (x))) 3=λfx.f (f (fx))... n n(f, x) =f n (x) n = λfx.f n x (+ mn) fx= f m+n x = mf (nfx) +=λmnfx.mf (nfx) ( mn) fx= f m n x =(f n ) m x = m(nf )x = λmnfx.m(nf )x PSU CS320 Fall 17 Week 4: Functional Programming 34 / 66

35 Values and Operators Church encoding for Booleans Define true and false as functions of two parameters: true selects the first parameter, and false selects the second parameter. T = λxy.x F = λxy.y not = λx.xft and = λxy.xyf or = λxy.xty Let s verify the definition of not. First, notice that TPQ =(λxy.x)pq (λy.p)q P FPQ =(λxy.y)pq (λy.y)q Q Now not T = (λx.xft)t TFT F not F = (λx.xft)f FFT T PSU CS320 Fall 17 Week 4: Functional Programming 35 / 66

36 Applied Lambda Calculus To make the task of specifying computation easier, and make long lambda expressions more readable, we would like to include values and operators into lambda calculus. The result: Applied Lambda Calculus = Pure Lambda Calculus +ValuesandOperators Applied lambda calculus is exactly equivalent to pure lambda calculus At same time, it looks more like a programming language PSU CS320 Fall 17 Week 4: Functional Programming 36 / 66

37 Let Binding Recall the let binding, let x = M in N, which declares that x has value M in the body N. It can be regarded as syntactic sugar for a combination of lambda abstraction and application: let x = M in N (λx.n)m PSU CS320 Fall 17 Week 4: Functional Programming 37 / 66

38 Y Combinator Question: A lambda abstraction defines an anonymous function. How can one define recursion with only anonymous functions? Answer: Y combinator (discovered by Haskell B. Curry) Y = λt.(λx.t(xx))(λx.t(xx)) Yf =(λx.f (xx))(λx.f (xx)) = f ((λx.f (xx))(λx.f (xx))) = f (Yf ) Yf = f (Yf ) Thus Yf is a fixed point of any function f. PSU CS320 Fall 17 Week 4: Functional Programming 38 / 66

39 Y Combinator Example: Define an anonymous factorial function. 1. Start with the normal self-recursive form: fac = λn.if (n = 0) then 1 else n fac(n 1) 2. Define a function G to represent the name fac a parameter: G = λf.λn.if (n = 0) then 1 else n f (n 1) 3. Hence fac = Gfac 4. We need to find a fixed point of G usethey combinator: Y G = G(Y G) fac = Y G = (λt.(λx.t(x x))(λx.t(x x))) λf.λn.if (n = 0) then 1 else n f (n 1) * Constructs such as if-then-else can also be defined by lambda expression. PSU CS320 Fall 17 Week 4: Functional Programming 39 / 66

40 Performing Computation Applied lambda calculus is very much a programming language. We can define functions and expressions for real computation problems. Question: How is a lambda calculus program executed? For instance, if we write the expression fac 5, how do we get the result 120? Answer: Lambda calculus uses two reductions to perform computation: α-reduction β-reduction PSU CS320 Fall 17 Week 4: Functional Programming 40 / 66

41 Free and Bound Variables Variables are either free or bound. The symbol λ is a binding operator. Example: Variable x is free in expression x +3 Variable x is bound in expression λx.x +3 We need to be careful about the scope of variables a variable can be both free and bound in a single expression. Example: In the following expression, the first y is bound, the second y is free: (λxy.y + 3)(x + y) PSU CS320 Fall 17 Week 4: Functional Programming 41 / 66

42 Alpha Reduction We can rename bound variables without changing the semantics of a lambda expression. Example: Function λx.x + y is the same as λz.z + y. The two expressions are said to be α-equivalent The process of renaming is called α-reduction We use [y/x]m to mean replace every occurrence of x in M with y. With this notation, we can express α-reduction formally as λx.m = λy.[y/x]m (α-reduction) PSU CS320 Fall 17 Week 4: Functional Programming 42 / 66

43 Beta Reduction The process of performing a function application is called β-reduction. It is the main computation rule in lambda calculus. (λx.m)n =[N/x]M (β-reduction) The two expressions are called β-equivalent Examples: (λx.x)y =[y/x]x = y (λx.xy)y =[y/x]xy = yy (λx.xy)λx.x =[λx.x/x]xy =(λx.x)y = y PSU CS320 Fall 17 Week 4: Functional Programming 43 / 66

44 Beta Reduction Sometimes we need to perform α-reduction before we can perform β-reduction. Example: Consider (λyx.x + y)(x + 1). Wrong: (λyx.x + y)(x + 1) =[x +1/y]λx.x + y = λx.x + x +1 Free variable x becomes bound after reduction. Correct: (λyx.x + y)(x + 1) =(λyz.[z/x]x + y)(x + 1) =(λyz.z + y)(x + 1) =[x +1/y]λz.z + y = λz.z + x +1 Free variable x stays free after reduction. PSU CS320 Fall 17 Week 4: Functional Programming 44 / 66

45 Normal Form and Confluence Normal Form Aλ-expression is in normal form if it cannot be further reduced by β-reduction. Example: (λy.yy)((λx.xx)a) ((λx.xx)a)((λx.xx)a) (aa)(aa) Confluence Regardless of the order in which the reductions are performed, a λ-expression always gets reduced to a unique normal form. Example: Another reduction sequence leads to the same normal form. (λy.yy)((λx.xx)a) (λy.yy)(aa) (aa)(aa) Evaluation in lambda calculus is order independent. PSU CS320 Fall 17 Week 4: Functional Programming 45 / 66

46 Lisp The first functional language, developed by John McCarthy in directly based on lambda calculus Very simple syntax prefix notation with (lots of) parentheses very few reserved words and predefined constructs Clean semantics described by a simple abstract machine model (written in Lisp itself) Historical contributions abstract view of memory cells instead of linear addresses programs as data sharing the same syntax garbage collection PSU CS320 Fall 17 Week 4: Functional Programming 46 / 66

47 Lisp Dialects Lisp has two main dialects: Common Lisp: large and complex allows both static and dynamic scopes large number of data structures arrays, records, complex numbers, packages, etc. Scheme: keeps Lisp s simple syntax and semantics static scope (different from Lisp) We ll use Scheme to show our examples. PSU CS320 Fall 17 Week 4: Functional Programming 47 / 66

48 Scheme Syntax All programs and data in Scheme are considered expressions. There are (only) two types of expressions: atoms and lists. expr list atom list ( {expr} ) atom number string symbol char boolean PSU CS320 Fall 17 Week 4: Functional Programming 48 / 66

49 Atoms Numbers: 42, -12, 3.14 Strings: "hello" Symbols: x, y, +, * Characters: #\a, #\b, #\c Booleans: #t, #f Value of an atom is just the object itself. PSU CS320 Fall 17 Week 4: Functional Programming 49 / 66

50 Atoms Built-in functions for manipulating atoms: Numbers: +, -, *, /, <, >, <=, >=, = Booleans: and, or, not Strings: string-length, string=? Question: These function names themselves are expressions. What are their values? Answer: Their values are (anonymous) functions. + #{procedure 110 +} * #{procedure 107 *} PSU CS320 Fall 17 Week 4: Functional Programming 50 / 66

51 List Expressions Lists are used to represent both data objects and expressions. List construction: (cons a ()) (a) (cons b (cons a ()) (b a) (cons b a) (b. a) (list a b c) (a b c) Notice the difference between (b a) and (b. a). Thelatteriscalled a dotted pair. From the structure point of view, lists are just special forms of nested dotted pairs: (a b c d) = (a. (b. (c. (d. ())))) List De-construction: (car (a b c)) a (cdr (a b c)) (b c) PSU CS320 Fall 17 Week 4: Functional Programming 51 / 66

52 List Expressions When a list is representing an expression, its first item is special it either encodes a special action or represents a function. List expressions whose first item is a keyword are called special forms. Example: (define x 5) (lambda (x) (* x x)) These are some important keywords: define, lambda, if, cond, let, quote List expressions in the other form represent function applications. Example: (+ x y) (average x y) PSU CS320 Fall 17 Week 4: Functional Programming 52 / 66

53 List Expressions Arithmetic and logical expressions take the prefix form: (+ 1 2), (- x y), (not x) Most operators can take one or more operands: (+ 1), (+ 1 2), ( ),... (- 1), (- 1 2), ( ),... // subtract from 1st oprnd (or x), (or x y), (or x y z w v),.. A few operators are also defined where there is no operand: (+) => 0, (*) => 1, (and) => #t, (or) => #f A comparison between C and Scheme expressions: C: * (a==b) && (a!=0) (a b) c Scheme: (+ 1 (* 2 3)) ( ) ( ) (and (= a b) (not (= a 0))) (or a b c) PSU CS320 Fall 17 Week 4: Functional Programming 53 / 66

54 Scheme Semantics The meaning of a Scheme expression is given by the evaluation rules: 1. Atomic literals evaluate to themselves 2. Symbols other than keywords are variables; their values are looked up in the current environment 3. Special-form list expressions are evaluated by pre-defined special rules 4. Other list expressions are evaluated as function applications 4.1 Evaluate all of the subexpressions of the list (in any order) 4.2 Apply the function to the values of the arguments and return the result as the list s value Rules for function application: 1. If function is built-in, just do it 2. If function is user-defined, then evaluate its body with each formal parameter replaced by the corresponding actual argument value PSU CS320 Fall 17 Week 4: Functional Programming 54 / 66

55 Lambda Special Form Lambda special form is used to define function. It s directly taken from lambda calculus. Syntax: (lambda ({symbol}) expr) It can be used to define functions with any number of parameters. (lambda () 2) (lambda (x) (* x x)) (lambda (x y) (/ (+ x y) 2)) Semantics: No operands are evaluated The value of a lambda expression is a procedure object, whose parameters are given in the 1st operand, and its body is given by the 2nd operand PSU CS320 Fall 17 Week 4: Functional Programming 55 / 66

56 Define Special Form Define special form is used to bind name to value. Syntax: (define symbol expr) (define x 5) (define average (lambda (x y) (/ (+ x y) 2))) Semantics: Evaluate the 2nd operand (only) Bind the value to the name given in the 1st operand The value of the define expression itself is undefined Note: Pre-defined symbols can be bound to new values. (define + *) ; redefine + (+ 5 5) ; = 25 (!) This is not recommended! PSU CS320 Fall 17 Week 4: Functional Programming 56 / 66

57 If Special Form If special form is used for controlled selection between two values. Syntax: (if expr 1 expr 2 [expr 3 ]) (if (= a 0) 0 (/ 1 a)) ; if a=0 return 0 ; else return 1/a (if (list? x) (car x)) ; if x is a list, ; return its first item Semantics: Evaluate expr 1 first, if the value is true, evaluate expr 2 and use its value as the if expression s value else evaluate expr 3 and use its value as the if expression s value; if expr 3 is absent, the if expression s value is undefined PSU CS320 Fall 17 Week 4: Functional Programming 57 / 66

58 Cond Special Form Cond special form is used for controlled selection among multiple values. Syntax: (p i and e i all denote expressions) (cond (p 1 e 1 ) (p n e n ) [(else e n+1 )]) (cond ((< 2 1) 1) ((< 1 2) 2)) ; has value 2 (cond ((< 2 1) 1) ((< 3 2) 2)) ; is undefined (cond (diverge 1) (true 0)) ; is undefined (assume ; diverge is undefined) (cond (true 0) (diverge 1)) ; has value 0 Semantics: Evaluate p 1,...,p n in order The cond expression s value is the value of e k if p k is true and if for every i < k, thevalueofp i is false The exression is undefined if p 1,...,p k 1 are false and p k is undefined, regardless of the values of p k+1,...,p n If none of these p i sistrueandthereisanelse expr at the end, then the expression s value is the value of e n+1 ; otherwise it s undefined PSU CS320 Fall 17 Week 4: Functional Programming 58 / 66

59 Let Special Form Let special form is used to bind names to values within an expression. It provides a local environment and scope for a set of variable names. Syntax: (let ((var 1 expr 1 ) (var n expr n )) expr n+1 ) The first expr is a binding list. (let ((a 2) (b 3)) (+ a b)) (let ((average (lambda (x y) (/ (+ x y) 2)))) (average 96 99)) Semantics: Evaluate expr 1,...,expr n (in no particular order) Bind var 1,...,var n to their corresponding expr s value Evaluate expr n+1 ; use its value as the let expression s value Note: Let can be defined by lambda expression: (let (x val) expr) ((lambda (x) expr) val) PSU CS320 Fall 17 Week 4: Functional Programming 59 / 66

60 Quote Special Form Quote special form is used for representing data literals. Syntax: (quote expr) or expr 'a ; => symbol a (quote "hi") ; => string "hi" (quote (+ 2 3)) ; => (list '+ '2 '3) Semantics: No evaluation needs to be performed Quote expression s value is the literal form of expr PSU CS320 Fall 17 Week 4: Functional Programming 60 / 66

61 The Eval Function The Scheme eval function takes a quoted expression, and evaluates it. (eval '(+ 2 3)) ; => 5 (define a 'b) (define b 'c) (define c 50) (eval a) ; => c (eval (eval a)) ; => 50 PSU CS320 Fall 17 Week 4: Functional Programming 61 / 66

62 Programs as Data With quote and eval, we can easily construct a program as a list and evaluate it, and do this processing within a program. Example: John s bank account has an initial balance (amnt). He made a sequence of withdraws, and the withdraw amounts are kept in a list (wlist). Now John wants to calculate the remaining balance in his account. Conventional approach: Subtracting each withdraw amount from the initial amount. (define (balance amnt wlist) (cond ((null? wlist) amnt) (else (balance (- amnt (car wlist)) (cdr wlist))))) Assume amnt = 800and wlist = ( ) (balance 800 ( )) (balance 770 (40 50)) (balance 730 (50)) (balance 680 ()) amnt = 680 PSU CS320 Fall 17 Week 4: Functional Programming 62 / 66

63 Programs as Data Programs-as-data approach: Constructing a list with the subtraction operator; then evaluate the list. (define (balance amnt wlist) (cond ((null? wlist) amnt) (else (eval (cons '- (cons amnt wlist)))))) Assume amnt = 800and wlist = ( ) (balance 800 ( )) (eval (cons - (cons 800 ( )))) (eval (cons - ( ))) (eval ( )) 780 PSU CS320 Fall 17 Week 4: Functional Programming 63 / 66

64 Recursion Revisit It s easy to define recursive functions in Scheme: (define (! n) (if (= n 0) 1 (* n (! (- n 1))))) (! 5) => 120 However, Defining factorial through the Y-combinator also works: (define Y (lambda (f) (let ((w (lambda (x) (f (lambda (n) ((x x) n)))))) (w w)))) (define G (lambda (f) (lambda (n) (if (= n 0) 1 (* n (f (- n 1))))))) ((G (Y G)) 5) => 120 PSU CS320 Fall 17 Week 4: Functional Programming 64 / 66

65 Scheme Interpreter in Scheme Recall that an interactive interpreter has a read-eval-print loop (REPL) program structure. Scheme has built-in support for all four operations. So it is super easy to write a Scheme interpreter in Scheme: (define (repl) (display "repl>") (write (eval (read))) (repl)) ; display a prompt ; read-eval-print an expr ; loop (tail-recursive call) Of course, the hard work is done by the eval function. But even if we need to write it ourselves, it s not that hard since Scheme s syntax is very simple! PSU CS320 Fall 17 Week 4: Functional Programming 65 / 66

66 Summary Functional programming is one of the main programming paradigms. It relies on functions and expressions for specifying computation. Lambda calculus provides the theoretical foundation for functional programming. It is a universal model of computation any computable function can be expressed by it. The lambda form of anonymous function definition has been widely adopted in programming languages today, including imperative languages. Lisp is a very important functional language. Its unified syntax allows seamless switching between expressions and data objects. We have studied some important concepts, such as: side effect, referential transparency, higher-order functions, first-class functions, α and β reductions. PSU CS320 Fall 17 Week 4: Functional Programming 66 / 66

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

More information

Functional Programming

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

More information

Programming Languages

Programming Languages Programming Languages Lambda Calculus and Scheme CSCI-GA.2110-003 Fall 2011 λ-calculus invented by Alonzo Church in 1932 as a model of computation basis for functional languages (e.g., Lisp, Scheme, ML,

More information

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus. Variables and Functions. cs3723 1 Lambda Calculus Variables and Functions cs3723 1 Lambda Calculus Mathematical system for functions Computation with functions Captures essence of variable binding Function parameters and substitution Can

More information

Functional Languages. Hwansoo Han

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

More information

Imperative languages

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

More information

Functional Programming. Big Picture. Design of Programming Languages

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

More information

LECTURE 16. Functional Programming

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

More information

Functional Programming. Pure Functional Languages

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

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

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

More information

Functional Programming. Pure Functional Languages

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

More information

Functional Programming Languages (FPL)

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

More information

Chapter 11 :: Functional Languages

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

More information

CS 314 Principles of Programming Languages

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.

More information

FP Foundations, Scheme

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

More information

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

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

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

Functional Programming. Pure Functional Programming

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).

More information

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

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

More information

CSCC24 Functional Programming Scheme Part 2

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

More information

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

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

More information

Recursive Definitions, Fixed Points and the Combinator

Recursive Definitions, Fixed Points and the Combinator Recursive Definitions, Fixed Points and the Combinator Dr. Greg Lavender Department of Computer Sciences University of Texas at Austin Recursive Self-Reference Recursive self-reference occurs regularly

More information

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

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

More information

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21 Functions as data Massimo Merro 9 November 2011 Massimo Merro The Lambda language 1 / 21 The core of sequential programming languages In the mid 1960s, Peter Landin observed that a complex programming

More information

CS 242. Fundamentals. Reading: See last slide

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

More information

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

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

More information

Introduction to lambda calculus Part 3

Introduction to lambda calculus Part 3 Introduction to lambda calculus Part 3 Antti-Juhani Kaijanaho 2017-01-27... 1 Untyped lambda calculus... 2 Typed lambda calculi In an untyped lambda calculus extended with integers, it is required that

More information

CS 360 Programming Languages Interpreters

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

More information

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

Introduction to Scheme

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

More information

Weeks 6&7: Procedures and Parameter Passing

Weeks 6&7: Procedures and Parameter Passing CS320 Principles of Programming Languages Weeks 6&7: Procedures and Parameter Passing Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Weeks 6&7: Procedures and Parameter Passing 1 / 45

More information

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC330. Objects, Functional Programming, and lambda calculus CMSC330 Objects, Functional Programming, and lambda calculus 1 OOP vs. FP Object-oriented programming (OOP) Computation as interactions between objects Objects encapsulate mutable data (state) Accessed

More information

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

More information

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

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

More information

CS 314 Principles of Programming Languages

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

More information

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

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,

More information

Functional Programming and Haskell

Functional Programming and Haskell Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Use currying

More information

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson Introduction to Functional Programming in Racket CS 550 Programming Languages Jeremy Johnson 1 Objective To introduce functional programming in racket Programs are functions and their semantics involve

More information

Programming Language Pragmatics

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

More information

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

More information

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples: COMP80 Programming Languages Slides Courtesy of Prof. Sam Guyer Lambda Calculus Formal system with three parts Notation for functions Proof system for equations Calculation rules called reduction Idea:

More information

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

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,

More information

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland COMP 1130 Lambda Calculus based on slides by Jeff Foster, U Maryland Motivation Commonly-used programming languages are large and complex ANSI C99 standard: 538 pages ANSI C++ standard: 714 pages Java

More information

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017 Last Week: What is

More information

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242 Spring 2013 Foundations Yu Zhang Acknowledgement: modified from Stanford CS242 https://courseware.stanford.edu/pg/courses/317431/ Course web site: http://staff.ustc.edu.cn/~yuzhang/fpl Reading Concepts

More information

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

Chapter 1. Fundamentals of Higher Order Programming

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

More information

Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: Lisp Introduction

More information

1.3. Conditional expressions To express case distinctions like

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

More information

Denotational Semantics. Domain Theory

Denotational Semantics. Domain Theory Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning

More information

Fundamentals and lambda calculus

Fundamentals and lambda calculus Fundamentals and lambda calculus Again: JavaScript functions JavaScript functions are first-class Syntax is a bit ugly/terse when you want to use functions as values; recall block scoping: (function ()

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 3 2. Applications... 3 3. Examples... 4 4. FPL Characteristics:... 5 5. Lambda calculus (LC)... 6 5.1. LC expressions forms... 6 5.2. Semantic of

More information

More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems More Lambda Calculus and Intro to Type Systems Plan Heavy Class Participation Thus, wake up! Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems Overview Static, Dyamic

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

Introduction to the λ-calculus

Introduction to the λ-calculus Announcements Prelim #2 issues: o Problem 5 grading guide out shortly o Problem 3 (hashing) issues Will be on final! Friday RDZ office hours are 11-12 not 1:30-2:30 1 Introduction to the λ-calculus Today

More information

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

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,

More information

Lambda Calculus see notes on Lambda Calculus

Lambda Calculus see notes on Lambda Calculus 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,

More information

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia CPL 2016, week 10 Clojure functional core Oleg Batrashev Institute of Computer Science, Tartu, Estonia April 11, 2016 Overview Today Clojure language core Next weeks Immutable data structures Clojure simple

More information

Lambda Calculus LC-1

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

More information

Pure Lambda Calculus. Lecture 17

Pure Lambda Calculus. Lecture 17 Pure Lambda Calculus Lecture 17 Lambda Calculus Lambda Calculus (λ-calculus) is a functional notation introduced by Alonzo Church in the early 1930s to formalize the notion of computability. Pure λ-calculus

More information

VU Semantik von Programmiersprachen

VU Semantik von Programmiersprachen VU Semantik von Programmiersprachen Agata Ciabattoni Institute für Computersprachen, Theory and Logic group (agata@logic.at) (A gentle) Introduction to λ calculus p. 1 Why shoud I studyλcalculus? p. 2

More information

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) 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:

More information

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Topic III LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Chapters 5( 4.5) and 13( 1) of Programming languages: Design and

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 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,

More information

Chapter 15 Functional Programming Languages

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.

More information

Shell CSCE 314 TAMU. Haskell Functions

Shell CSCE 314 TAMU. Haskell Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions can

More information

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine. CMSC 330: Organization of Programming Languages Lambda Calculus Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying or tuples

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying

More information

SOFTWARE ARCHITECTURE 6. LISP

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

More information

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08 Introduction to Lambda Calculus Lecture 5 CS 565 1/24/08 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

Functional programming in LISP

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

More information

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Introduction to Lambda Calculus. Lecture 7 CS /08/09 Introduction to Lambda Calculus Lecture 7 CS 565 02/08/09 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

Control in Sequential Languages

Control in Sequential Languages CS 242 2012 Control in Sequential Languages Reading: Chapter 8, Sections 8.1 8.3 (only) Section 7.3 of The Haskell 98 Report, Exception Handling in the I/O Monad, http://www.haskell.org/onlinelibrary/io-13.html

More information

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

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

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages Call-by-value Operational Semantics

More information

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus CMSC 330: Organization of Programming Languages Lambda Calculus 1 Turing Completeness Turing machines are the most powerful description of computation possible They define the Turing-computable functions

More information

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus 1 Computer Science 203 Programming Languages Fall 2004 Lecture 10 Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus Plan Informal discussion of procedures and bindings Introduction

More information

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

CS 11 Haskell track: lecture 1

CS 11 Haskell track: lecture 1 CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of

More information

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems More Lambda Calculus and Intro to Type Systems #1 One Slide Summary The lambda calculus is a model of computation or a programming language that is as expressive as a Turing machine. The lambda calculus

More information

User-defined Functions. Conditional Expressions in Scheme

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

More information

Documentation for LISP in BASIC

Documentation for LISP in BASIC Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Tuesday, February 19, 2013 The lambda calculus (or λ-calculus) was introduced by Alonzo Church and Stephen Cole Kleene in

More information

Lambda Calculus. Gunnar Gotshalks LC-1

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

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

10.6 Theoretical Foundations

10.6 Theoretical Foundations 10 Functional Languages 10.6 Theoretical Foundations EXAMPLE 10.44 Functions as mappings Mathematically,a function is a single-valued mapping: it associates every element in one set (the domain) with (at

More information

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program

More information

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

More information

Symbolic Computation and Common Lisp

Symbolic Computation and Common Lisp Symbolic Computation and Common Lisp Dr. Neil T. Dantam CSCI-56, Colorado School of Mines Fall 28 Dantam (Mines CSCI-56) Lisp Fall 28 / 92 Why? Symbolic Computing: Much of this course deals with processing

More information

Lists. Michael P. Fourman. February 2, 2010

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

More information

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

More information