Concepts of Programming Languages

Similar documents
Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages

15. Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Example Scheme Function: equal

Chapter 15. Functional Programming Languages ISBN

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming Languages (FPL)

Concepts of Programming Languages

Programming Languages, Summary CSC419; Odelia Schwartz

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

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Chapter 15 Functional Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

FP Foundations, Scheme

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

Chapter 11 :: Functional Languages

Functional Programming Languages (FPL)

Introduction. A. Bellaachia Page: 1

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

Imperative languages

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

CS 314 Principles of Programming Languages

Functional Programming

CS 11 Haskell track: lecture 1

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

Functional Programming

A Brief Introduction to Scheme (I)

Introduction to Functional Programming in Haskell 1 / 56

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

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Stop coding Pascal. Saturday, April 6, 13

JVM ByteCode Interpreter

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

CS 314 Principles of Programming Languages

LECTURE 16. Functional Programming

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

Com S 541. Programming Languages I

Refactoring to Functional. Hadi Hariri

SD314 Outils pour le Big Data

Programming Language Pragmatics

(Refer Slide Time: 4:00)

Software II: Principles of Programming Languages. Why Expressions?

Robot Programming with Lisp

Functional Programming

G Programming Languages - Fall 2012

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

Functional programming in LISP

Programming Paradigms

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

Introduction to OCaml

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

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Functional Programming and Haskell

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

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

Lecture 19: Functions, Types and Data Structures in Haskell

Tomas Petricek. F# Language Overview

3. Functional Programming. Oscar Nierstrasz

Lecture 13 CIS 341: COMPILERS

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Functional programming in C#

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

Functional Programming

Seminar in Programming Languages

CSCI-GA Scripting Languages

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Functional Programming for Imperative Programmers

Introduction to Functional Programming and Haskell. Aden Seaman

Functional Programming. Another representative from the Declarative paradigm

Functional Programming Principles in Scala. Martin Odersky

Functional Programming Lecture 1: Introduction

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

INSTITUTE OF AERONAUTICAL ENGINEERING

Functional Programming

Functional Languages. Hwansoo Han

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

Chapter 7. Expressions and Assignment Statements ISBN

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

PL Categories: Functional PLs Introduction to Haskell Haskell: Functions

Classical Themes of Computer Science

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

Functional Programming Lecture 13: FP in the Real World

Programming Languages 2nd edition Tucker and Noonan"

Chapter 7. Expressions and Assignment Statements

CSCI.4430/6969 Programming Languages Lecture Notes

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

Functional Programming. Pure Functional Programming

CS 415 Midterm Exam Spring 2002

VU Semantik von Programmiersprachen

Concepts behind the C# 3 language. Faculty of Mathematics and Physics, Charles University in Prague

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

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

CMSC330. Objects, Functional Programming, and lambda calculus

CSE341, Spring 2013, Final Examination June 13, 2013

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

Chapter 2. Evolution of the Major Programming Languages

More Lambda Calculus and Intro to Type Systems

Transcription:

Concepts of Programming Languages Lecture 15 - Functional Programming Patrick Donnelly Montana State University Spring 2014 Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 1 / 26

Administrivia Assignments: Homework #3 : due 03.31 Reading: Chapter 15 Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 2 / 26

It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 3 / 26

Introduction The design of the imperative languages is based directly on the von Neumann architecture Efficiency is the primary concern, rather than the suitability of the language for software development The design of the functional languages is based on mathematical functions A solid theoretical basis that is also closer to the user, but relatively unconcerned with the architecture of the machines on which programs will run Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 4 / 26

Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions: domain = input, range = output Variables are mathematical symbols: not associated with memory locations. Pure functional programming is state-free: no assignment Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 5 / 26

Mathematical Functions Definition A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set. Example The function Square has R (the reals) as domain and range. Square: R R Square(n): n 2 Total Function A function is total if it is defined for all values of its domain. Otherwise, it is partial. E.g., Square is total. Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 6 / 26

Functional Forms Definition A higher-order function, or functional form, is one that either takes functions as parameters or yields a function as its result, or both Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 7 / 26

Functional Composition A functional form that takes two functions as parameters and yields a function whose value is the first actual parameter function applied to the application of the second. Form: h f g which means h(x) f (g(x)) Example For f (x) x + 2 and g(x) 3 x, h f g yields (3 x) + 2 Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 8 / 26

Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained by applying the given function to each element of a list of parameters: Form: α Example For h(x) x x α(h, (2, 3, 4)) yields (4, 9, 16) Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 9 / 26

Fundamentals of Functional Programming Languages The objective of the design of a FPL is to mimic mathematical functions to the greatest extent possible The basic process of computation is fundamentally different in a FPL than in an imperative language In an imperative language, operations are done and the results are stored in variables for later use Management of variables is a constant concern and source of complexity for imperative programming Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 10 / 26

Fundamentals of Functional Programming Languages In an FPL, variables are not necessary, as is the case in mathematics Definition Referential transparency: a function s result depends only upon the values of its parameters, thus the evaluation of a function always produces the same result given the same parameters. Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 11 / 26

LISP Data Types and Structures Data object types: originally only atoms and lists List form: parenthesized collections of sublists and/or atoms e.g., (A B (C D) E) Originally, LISP was a typeless language LISP lists are stored internally as single-linked lists Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 12 / 26

LISP Interpretation Lambda notation is used to specify functions and function definitions. Function applications and data have the same form. Example If the list (A B C) is interpreted as data it is a simple list of three atoms, A, B, and C If it is interpreted as a function application, it means that the function named A is applied to the two parameters, B and C The first LISP interpreter appeared only as a demonstration of the universality of the computational capabilities of the notation Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 13 / 26

Common LISP A combination of many of the features of the popular dialects of LISP around in the early 1980s A large and complex language the opposite of Scheme Features include: records arrays complex numbers character strings powerful I/O capabilities packages with access control iterative control statements Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 14 / 26

ML A static-scoped functional language with syntax that is closer to Pascal than to LISP Uses type declarations, but also does type inferencing to determine the types of undeclared variables It is strongly typed (whereas Scheme is essentially typeless) and has no type coercions Does not have imperative-style variables Its identifiers are untyped names for values Includes exception handling and a module facility for implementing abstract data types Includes lists and list operations Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 15 / 26

Currying ML functions actually take just one parameter if more are given, it considers the parameters a tuple (commas required) Definition Process of currying replaces a function with more than one parameter with a function with one parameter that returns a function that takes the other parameters of the original function An ML function that takes more than one parameter can be defined in curried form by leaving out the commas in the parameters fun add a b = a + b; A function with one parameter, a. Returns a function that takes b as a parameter. Call: add 3 5; Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 16 / 26

Partial Evaluation Curried functions can be used to create new functions by partial evaluation Partial evaluation means that the function is evaluated with actual parameters for one or more of the leftmost actual parameters fun add5 x add 5 x; Takes the actual parameter 5 and evaluates the add function with 5 as the value of its first formal parameter. Returns a function that adds 5 to its single parameter val num = add5 10; (* sets num to 15 *) Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 17 / 26

Haskell Similar to ML (syntax, static scoped, strongly typed, type inferencing, pattern matching) Different from ML (and most other functional languages) in that it is purely functional (e.g., no variables, no assignment statements, and no side effects of any kind) Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 18 / 26

Lazy Evaluation Definition A language is strict if it requires all actual parameters to be fully evaluated. Definition A language is nonstrict if it does not have the strict requirement Nonstrict languages are more efficient and allow some interesting capabilities infinite lists Definition Lazy evaluation = delaying argument evaluation in a function call until the argument is needed. Advantage: flexibility Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 19 / 26

F# Based on Ocaml, which is a descendant of ML and Haskell Fundamentally a functional language, but with imperative features and supports OOP Has a full-featured IDE, an extensive library of utilities, and interoperates with other.net languages Includes tuples, lists, discriminated unions, records, and both mutable and immutable arrays Supports generic sequences, whose values can be created with generators and through iteration Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 20 / 26

Why F# is Interesting It builds on previous functional languages It supports virtually all programming methodologies in widespread use today It is the first functional language that is designed for interoperability with other widely used languages At its release, it had an elaborate and well-developed IDE and library of utility software Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 21 / 26

Support for Functional Programming Support for functional programming is increasingly creeping into imperative languages: Anonymous functions (lambda expressions) JavaScript: leave the name out of a function definition C#: i => (i % 2) == 0 (returns true or false depending on whether the parameter is even or odd) Python: lambda a, b : 2 * a - b Python supports the higher-order functions filter and map (often use lambda expressions as their first parameters) map(lambda x : x ** 3, [2, 4, 6, 8]) Returns [8, 64, 216, 512] Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 22 / 26

Support for Functional Programming Python supports partial function applications from operator import add add5 = partial (add, 5) (the first line imports add as a function) Use: add5(15) Ruby Blocks Are effectively subprograms that are sent to methods, which makes the method a higher-order subprogram A block can be converted to a subprogram object with lambda times = lambda a, b a * b Use: x = times.(3, 4) (sets x to 12) Times can be curried with times5 = times.curry.(5) Use: x5 = times5.(3) (sets x5 to 15) Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 23 / 26

Comparing Functional and Imperative Languages Imperative Languages: Efficient execution Complex semantics Complex syntax Concurrency is programmer designed Functional Languages: Simple semantics Simple syntax Less efficient execution Programs can automatically be made concurrent Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 24 / 26

Summary Functional programming languages use function application, conditional expressions, recursion, and functional forms to control program execution LISP began as a purely functional language and later included imperative features Scheme is a relatively simple dialect of LISP that uses static scoping exclusively Common LISP is a large LISP-based language ML is a static-scoped and strongly typed functional language that uses type inference Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 25 / 26

Summary Haskell is a lazy functional language supporting infinite lists and set comprehension. F# is a.net functional language that also supports imperative and object-oriented programming Some primarily imperative languages now incorporate some support for functional programming Purely functional languages have advantages over imperative alternatives, but still are not very widely used Patrick Donnelly (Montana State University) Concepts of Programming Languages Spring 2014 26 / 26