Functional Programming

Similar documents
Functional Programming. Big Picture. Design of Programming Languages

Programming Language Pragmatics

Functional Languages. Hwansoo Han

Chapter 11 :: Functional Languages

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

15. Functional Programming

CSCE 314 Programming Languages

Introduction. chapter Functions

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

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

LECTURE 16. Functional Programming

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

3. Functional Programming. Oscar Nierstrasz

Lambda Calculus. Gunnar Gotshalks LC-1

CS 314 Principles of Programming Languages

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

CS 242. Fundamentals. Reading: See last slide

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Imperative languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

More Lambda Calculus and Intro to Type Systems

Programming Languages 2nd edition Tucker and Noonan"

Concepts of Programming Languages

Functional Programming

Functional Programming. Pure Functional Languages

Functional Programming Principles in Scala. Martin Odersky

CS 11 Haskell track: lecture 1

Chapter 15. Functional Programming Languages

Introduction. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

More Lambda Calculus and Intro to Type Systems

Lambda Calculus see notes on Lambda Calculus

Lambda Calculus LC-1

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

Formal Systems and their Applications

Functional Programming. Pure Functional Languages

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

Welcome to. Instructor Marc Pomplun CS 470/670. Introduction to Artificial Intelligence 1/26/2016. Spring Selectivity in Complex Scenes

Introduction to OCaml

CS 314 Principles of Programming Languages

More Lambda Calculus and Intro to Type Systems

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Introduction to Functional Programming in Haskell 1 / 56

G Programming Languages - Fall 2012

Functional Programming

Concepts of Programming Languages

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

ITT8060 Advanced Programming

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Functional Programming Languages (FPL)

Expression Evaluation and Control Flow. Outline

Presented By Andrew Butt

Simply-Typed Lambda Calculus

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

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Introduction to Functional Programming and Haskell. Aden Seaman

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Functional Programming

Programming Languages

CS 6110 S14 Lecture 1 Introduction 24 January 2014

JVM ByteCode Interpreter

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Side Effects (3A) Young Won Lim 1/13/18

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

An introduction to functional programming. July 23, 2010

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

Introduction to SML Getting Started

Functional Programming

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

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

Lecture 15: Iteration and Recursion

CSE 341: Programming Languages

CSC324 Functional Programming Efficiency Issues, Parameter Lists

COSE212: Programming Languages. Lecture 0 Course Overview

A Pragmatic Case For. Static Typing

Weeks 6&7: Procedures and Parameter Passing

Lambda Calculus. Variables and Functions. cs3723 1

Scheme Datatypes. expressions. pairs. atoms. lists. symbols. booleans

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CS457/557 Functional Languages

Functional Programming

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Com S 541. Programming Languages I

Software II: Principles of Programming Languages. Why Expressions?

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Lecture 13 CIS 341: COMPILERS

Programming Languages

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

(Refer Slide Time: 4:00)

CS383 PROGRAMMING LANGUAGES. Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

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

Introduction to Functional Programming

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Organization of Programming Languages CS3200/5200N. Lecture 11

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

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

Transcription:

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 Overview First, some introductory remarks. Then weʼll cover Haskell details in a tutorial style. Take notes. Ask questions. 2

What s a functional language? Most functional languages provide: Functions as first-class values Higher-order functions Primitive list type (operators on lists) Recursion Structured function return (return tuples) Garbage collection Polymorphism and type inference Covered next lecture. Functional programming. Also possible in imperative languages. Applying functional style to imperative language can yield very elegant code. 3

So Why Functional? Teaches truly recursive thinking. Teaches good programming style. Short, self-contained functions. Implicit Polymorphism. Natural expressiveness for symbolic and algebraic computations. Algorithms clearly map to the code that implements them. 4

Origins Lambda-calculus as semantic model (Church) LISP (1958, MIT, McCarthy) Alanzo Church (defun fib (n) (if (or (= n 0) (= n 1)) 1 (+ (fib (- n 1)) (fib (- n 2))))) Naive Fibonacci Implementation in LISP John McCarthy Source: Wikimedia Commons 5

Can Programming be Liberated from the von Neumann Style? This is the title of the lecture given by John Backus when he received the Turing Award in 1977. In this, he pointed out that a program should be an abstract description of algorithms rather than a sequence of changes in the state of the memory. He called for raising the level of abstraction A way to realize this goal is functional programming Programs written in modern functional programming languages are a set of mathematical relationships between objects. No explicit memory management takes place. 6

History Lisp Dynamic Scoping Common Lisp (CL), Scheme Static scoping ML (late 1970ies) Common Lisp Logo (unofficial) Scheme Logo Typing, type inference, fewer parentheses Haskell, Miranda (1980ies) Haskell Logo purely functional Compiler disallows side effects 7

Referential Transparency Bindings are immutable. Any name may be substituted by the value bound to that name and not alter the semantics of the expression. no side effects. This means no printf() debugging! Functional programing languages encourage referential transparency. Pure functional programming languages enforce referential transparency. 8

Referential Transparency If two expressions are defined to have equal values, then one can be substituted for the other in any expression without affecting the result of the computation. sumofsquares :: Int -> Int -> Int sumofsquares x y = let x2 = x * x y2 = y * y in x2 + y2 sumofsquares' :: Int -> Int -> Int sumofsquares' x y = (x * x) + (y * y) Semantically equivalent. 9

Referentially Transparent Functions aka pure functions A function is called referentially transparent if given the same argument(s), it always returns the same result. In mathematics, all functions are referentially transparent. In programming this is not always the case, with use of imperative features in languages. The subroutine/function called could affect some global variable that will cause a second invocation to return a different value. Input/Output 10

Evaluation (Scott) Advantages Lack of side effects makes programs easier to understand. Lack of explicit evaluation order (in some languages) offers possibility of parallel evaluation (e.g. Haskell). Programs are often surprisingly concise. Language can be extremely small and yet powerful. Problems Difficult (but not impossible!) to implement efficiently on von Neumann machines. Naive impl.: Lots of copying, inefficient cache use, memory use. Requires a different mode of thinking by the programmer. Not necessarily a bad thing! Difficult to integrate I/O into purely functional model. Haskell: Monads. 11