FUNCTIONAL PROGRAMMING (1) PROF. SIMON PARSONS

Similar documents
Functional Programming

Lists. Michael P. Fourman. February 2, 2010

Intro. Scheme Basics. scm> 5 5. scm>

CS 11 Haskell track: lecture 1

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

Functional Programming

Functional Programming. Pure Functional Languages

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Introduction to Functional Programming in Haskell 1 / 56

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming with Math and Logic

CS115 - Module 9 - filter, map, and friends

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

6.001 Notes: Section 8.1

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

Functional Languages. Hwansoo Han

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Functional Programming. Pure Functional Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Documentation for LISP in BASIC

CSCC24 Functional Programming Scheme Part 2

LECTURE 16. Functional Programming

Chapter 11 :: Functional Languages

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

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

Programming Languages

Functional Programming Languages (FPL)

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

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

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

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

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

6.001 Notes: Section 6.1

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

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

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

Lecture 19: Functions, Types and Data Structures in Haskell

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

Adding Machine Run 2

Functional Programming. Big Picture. Design of Programming Languages

CS558 Programming Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Software Paradigms (Lesson 4) Functional Programming Paradigm

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Lambda Calculus LC-1

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

CPS 506 Comparative Programming Languages. Programming Language Paradigm

A general introduction to Functional Programming using Haskell

An introduction introduction to functional functional programming programming using usin Haskell

QUIZ. What is wrong with this code that uses default arguments?

Functional Programming

User-defined Functions. Conditional Expressions in Scheme

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

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

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

Racket Style Guide Fall 2017

Lambda Calculus. Gunnar Gotshalks LC-1

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

printf( Please enter another number: ); scanf( %d, &num2);

Graphical Untyped Lambda Calculus Interactive Interpreter

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CSE 3302 Programming Languages Lecture 8: Functional Programming

These are notes for the third lecture; if statements and loops.

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

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

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

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

Z Notation. June 21, 2018

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

(Refer Slide Time: 01:01)

Introduction to Functional Programming

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

Mini-ML. CS 502 Lecture 2 8/28/08

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

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

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

Expressions and Assignment

Chapter 6 Control Flow. June 9, 2015

A Small Interpreted Language

Chapter 15. Functional Programming Languages

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

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

Haskell Overview II (2A) Young Won Lim 8/9/16

Functional programming with Common Lisp

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Lecture 4: Higher Order Functions

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Case Study: Undefined Variables

Type-indexed functions in Generic Haskell

Imperative languages

Chapter 15. Functional Programming Languages

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Transcription:

FUNCTIONAL PROGRAMMING (1) PROF. SIMON PARSONS

Imperative programming is concerned with how. Functional or applicative programming is, by contrast, concerned with what. It is based on the mathematics of the lambda calculus (Church as opposed to Turing). Programming without variables. It is inherently concise, elegant, and difficult to create subtle bugs in. Functional programming Lecture 1 2

Referential transparency The main (good) property of functional programming is referential transparency. Every expression denotes a single value. This value cannot be changed by evaluating an expression or by sharing it between different parts of the program. There can be no reference to global data. (Indeed there is no such thing as global data.) There are no side-effects, unlike in referentially opaque languages. Functional programming Lecture 1 3

program example(output) var flag: boolean function f(n:int): int begin if flag then f:= n else f: 2*n flag := not flag end begin flag := true writeln(f(1) + f(2)) writeln(f(2) + f(1)) end Functional programming Lecture 1 4

What is the output? Functional programming Lecture 1 5

Okay, so the answer is 5 followed by 4. This is odd since if these were mathematical functions, for any f. f(1) + f(2) = f(2) + f(1) But this is because mathematical functions are functions only of their inputs. They have no memory. We can always tell what the value of a mathematical function will be just from its inputs. Functional programming Lecture 1 6

At the heart of the problem is fact that the global data flag controls the value of f. In particular the assignment: flag := not flag is the thing that gives this behaviour. If we eliminate assignment, we eliminate this kind of behaviour. Variables are no longer placeholders for values that change. (They are much less variable than variables in imperative programs). Functional programming Lecture 1 7

Simple functional programming in HOPE We start with a function that squares numbers. In the rather odd syntax of HOPE this is: dec square: num -> num; --- square(x) <= x * x; Since we aren t really interested in HOPE, we won t explain the syntax in any great detail. Note though that first line includes a type definition. Functional programming Lecture 1 8

HOPE is strongly typed. Other functional languages aren t typed (LISP for example). We call the function by: square(3) Which evaluates to 3 * 3 by definition, and then to 9 by the definition of *. Note only that, it will always evaluate to 9. Functional programming Lecture 1 9

More complex functions: dec max : num # num -> num; --- max(m, n) <= if m > n then m else n; and: dec max3 : num # num # num -> num ---max3(a, b, c) <= max(a, max(b, c)); The type definitions indicate that the functions take two and three arguments respectively. Functional programming Lecture 1 10

Tuples Saying that these functions take two and three arguments is slightly misleading. Instead they both have one argument they are both tuples. One is a two-tuple and one is a three-tuple. This has one neat advantage you can get functions to return a tuple, and thus several values. dec IntDiv : num # num -> num # num; --- IntDiv(m, n) <= (m div n, m mod n); And we can the compose max(intdiv(11, 4)), which will give 3. Functional programming Lecture 1 11

Another function: dec analyse : real -> char # trueval # num; ---analyse(r) <= (if r < 0 then - else +, (r > = -1.0) and (r=< 1.0), round(r)); Applying analyse(-1.04) will give ( -, false, -1) Note the overloading of >. Functional programming Lecture 1 12

Recursion Without variables, we can t write functional programs with loops. So to get iteration, we need recursion. dec sum : num -> num; ---sum(n) <= if n = 0 then 0 else sum(n - 1) + n; Which works in the same way as recursion normally does. Recursion fits in perfectly with the functional approach. Each application of the recursive function is referentially transparent and easy to establish the value of. Functional programming Lecture 1 13

Here is a classic recursive function, with a twist. We can define functions to be infix. Here is the power function as an infix function: infix ˆ : 7; dec ˆ : num # num -> num; --- x ˆ y <= if y = 0 then 1 else x * x ˆ (y - 1); Again, HOPE gives us a very elegant way of defining the function. Functional programming Lecture 1 14

Qualified expressions Because we don t have variables, sometime it seems we have to do unecessary work when evaluating functions: dec f: num -> num; ---f(x) <= g(square(max(x, 4))) + (if x <= 1 then 1 else g(square(max(x, 4)))); Here we have to evaluate g(square(max(x, 4))) twice in some situations. With variables, of course, we would have to do this just once. Functional programming Lecture 1 15

Once way around this would be to define the repeated bit as a new function: dec f: num -> num; ---f(x) <= f1(g(square(max(x, 4)))) dec f1: num -> num; ---f1(a, b) <= a + (if b =< 1 then 1 else a) Efficiency here relies on efficient evaluation in the language. Another way is to use qualified expressions. Functional programming Lecture 1 16

Consider: dec f : num -> num --- f(x) <= let a == g(square(max(x, 4))) in a + (if x =< 1 then 1 else a)) The ľet construct allows us to extend the set of parameters of a function. In general: let <name> == <expression1> in <expression2> The first expression defines <name> and the second uses it. Functional programming Lecture 1 17

We also have: <expression2> where <name> == <expression1> So we could also write: dec f : num -> num --- f(x) <= a + (if x =< 1 then 1 else a)) where a == g(square(max(x, 4))) Note that == associates a name with an expression, it does not do assignment. Functional programming Lecture 1 18

To see this: let x == E1 in if (let x == E2 in E3) then x else 1 + x The first let associates E1 with x. The second let doesn t change this. Instead it renames E2 as x within E3. Outside E3 x has its original meaning. So far we have used qualified expressions to save on evaluation. Functional programming Lecture 1 19

We also use them to clarify functions. A third use is to decompose tuples. dec quot : num # num -> num; --- quot(q, r) <= q; dec rem : num # num -> num; --- rem(q, r) < = r; let pair == IntDiv(x, y) in quot(pair) * y + rem(pair) let(q, r) == IntDiv(x, y) in q * y + r This latter expression pattern matches (q, r) with the result of calling IntDiv. Functional programming Lecture 1 20

User defined data As in most languages, we can t do much interesting stuff in HOPE without defining data. This is way simpler in HOPE than in other languages. Consider handling lists. In C, we have to use structs, and pointers and worry about memory. Even in Java we have to use the right constructors. In HOPE we just deal with the recursive definition of a list. Functional programming Lecture 1 21

A list is either empty or an element followed by a list. data NumList == nil ++ cons(num # NumList) Here nil and cons are constructors. A single element list is then: cons(3, nil) And the list comprising 1, 2 and 3 is: cons(1, cons(2, cons(3, nil))) Functional programming Lecture 1 22

To define another kind of list we just do something similar: data CharList == NilCharList ++ ConsChars(char # CharList) Note that there is nothing special about the names nil or cons. Note also that we don t have to say anything about how these lists are represented internally. All we tell HOPE is that the list is either a something or a character followed by a list. Functional programming Lecture 1 23

The similarity of the definitions is intentional. All list definitions look like this. In fact, we can make a general definition: typevar any data list(any) == AnyNil ++ AnyCons(any # list(any)) This is a polymorphic definition. We parameterize the list by the kinds of objects contained in it. Functional programming Lecture 1 24

With this definition we can build lists of any type: AnyCons(1, AnyCons(2, nil)) AnyCons( a, AnyCons( b, nil)) AnyCons(AnyNil, AnyCons(AnyCons(1, nil))) AnyNil The last two are a list of lists, and a list of unspecificed type. Functional programming Lecture 1 25

Lists are so common that they are built into HOPE. infix :: : 7 data list(alpha) == nil ++ alpha :: list(alpha) We can also write lists as, for example [1, 2, 3]. Strings are lists of characters. With this information it is easy to write functions to handle lists. Functional programming Lecture 1 26

dec join : list(alpha) # list(alpha) -> list(alpha); --- join(nil, L) <= L; --- join(x::y, L) <= x :: join(y, L) dec rev: list(alpha) -> list(alpha); --- rev(nil) <= nil; --- rev(x::l) <= rev(l) join [x]; Note that join is predefined in HOPE as the infix function <>. Functional programming Lecture 1 27

Consider Higher order functions dec IncList : list(num) -> list(num); --- IncList(nil) <= nil; --- IncList(x::l) <= (x + 1)::IncList(l); dec MakeStrings : list(char) -> list(list(char)); --- MakeStrings(nil) <= nil; --- MakeStrings(c::l) <= [c]::makestrings(l); While doing different things, these two functions have the same basic form. Functional programming Lecture 1 28

Both operate on a list and apply a function to every member of the list. The two functions are: dec Inc : num -> num --- Inc(n) <= n + 1 dec Listify : char -> list(char) --- Listify(c) <= [c] We can capture this by defining a higher order function Functional programming Lecture 1 29

This takes a function and a list as arguments and applies the function to every member of the list. dec map : (alpha -> beta) # list(alpha) -> list(beta); --- map(f, nil) <= nil; --- map(f, x :: l) <= f(x) :: map(f, l); We can then write down the equivalent of our two earlier functions. map(inc, L) map(listify, L) Functional programming Lecture 1 30

Of course, this relies on us having defined Listify and Inc. However, we don t even have to do this. HOPE provides us with the means to write anonymous function bodies when and where we need them. For example: lambda x => x + 1 Here we have to use the word lambda. Functional programming Lecture 1 31

In general, we can replace any function with a lambda expression. We replace: --- f(x) <= E with lambda x => E Thus the function IncList is the same as: map(lambda x => x + 1, L) Functional programming Lecture 1 32

Note that we have problems defining a recursive lambda because there is no name to use in the recursion. Instead we have to use a let or where. For example: let f == lambda x => if x = 0 then 0 else x + f(x - 1) (which computes the sum of the first 3 numbers.) Such constructs are called recursive let and recursive where Functional programming Lecture 1 33

Some functional languages make these separate constructs (eg letrec). In HOPE lambda expressions can also contain a number of parts. --- IsEmpty(nil) <= true; --- IsEmpty(_::_) <= false; becomes lambda nil => true _::_ => false Functional programming Lecture 1 34