Functional Programming

Slides:



Advertisements
Similar presentations
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Advertisements

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Names and Bindings.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Language Evaluation Criteria
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Imperative Programming
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
ISBN Chapter 15 Functional Programming Languages.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CMSC 330: Organization of Programming Languages Operational Semantics.
ISBN Chapter 15 Functional Programming Languages.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
The for Statement A most versatile loop
Functional Programming
CS314 – Section 5 Recitation 9
Implementing Subprograms
Functional Programming
Chapter 10 : Implementing Subprograms
Chapter 10 Programming Fundamentals with JavaScript
Functional Programming Languages
Functional Programming Languages
Principles of programming languages 12: Functional programming
Implementing Subprograms
Expressions and Assignment
CS 326 Programming Languages, Concepts and Implementation
Subprograms The basic abstraction mechanism.
Memory Locations For Variables
Functional Programming Languages
Programmazione I a.a. 2017/2018.
Type Systems Terms to learn about types: Related concepts: Type
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Chapter 10 Programming Fundamentals with JavaScript
Object Oriented Programming COP3330 / CGS5409
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Scope, Visibility, and Lifetime
CSC 533: Programming Languages Spring 2015
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Implementing Subprograms
CS 36 – Chapter 11 Functional programming Features Practice
Chapter8: Statement-Level Control Structures April 9, 2019
Course Overview PART I: overview material PART II: inside a compiler
Scope, Function Calls and Storage Management
15.2 Mathematical Functions
CSE 341 Lecture 2 lists and tuples; more functions; mutable state
Chapter 15 Functional Programming 6/1/2019.
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Implementing Subprograms
Presentation transcript:

Functional Programming We already visited this when we looked at LISP We return to this topic to more formally discuss why functional programming is important in a separate set of notes, we look at a related topic, closure We look at several functional programming languages in a separate set of notes, we look at F# Most programming languages, whether OO or not, whether compiled or interpreted, are imperative languages program instructions change the state of the machine state is the value of the variables Functional programming instead focuses on the application on functions to compute no local variables (or if there are, no side effects) the book refers to “mathematical” functions, but that does not necessarily mean arithmetic functions, it is more meant a function in the mathematical sense x = f(y) meaning that we pass parameters and get a value/result back

Why Functional? More readable Easier to develop because code is likely to be independent of its context that is, you write a function independent of the code that might call upon it Recursion is simplified when you think in terms of a mathematical function and so functional programming *should* be easier this is probably not going to be true in most cases because its easier for us to write code using loops than using recursion More reliable because programs are simpler (fewer or no variables to have to understand) this is true if you are used to recursion, otherwise functional programming can look very cryptic More likely to be correct because it should be more readable, writable and reliable

The Lambda Expression Most functional languages include the lambda expression or a lambda function This is a function that is unnamed this allows you to define and invoke a function at any time, including from within an instruction (known as an in-line function) The notation mathematically looks like this (l(x) function code here) (parameter) you can have as many parameters as desired example: (l(x, y) if (x > y) x else y) (5, 10) returns 10 we briefly saw in LISP the use of a lambda expression when using mapcar, we will similarly see the use of lambda expressions in F# most functional languages allow you to formally define a function so that we do not *need* lambda expressions but can use them without the ability to define a function, we would have to write all functions as lambda expressions

Functional Programming Concepts Referential transparency execution of a function will always result in the same outcome when supplied the same parameters – that is, we have no global variables or side effects Lazy evaluation delay evaluation of an expression until it is needed this allows for calling a function only if needed and also can permit a function to be called once even though it may be invoked (on the same parameter) multiple times we often implement this feature using memoization, that is, by storing the result in a table to recall it for each successive function call (again remembering that we are talking about a function call that has the same params as a previous call) – also known as dynamic programming Currying taking a function which is passed multiple parameters into several functions, each of which are passed a single parameter (see nested functions on the next slide) First class functions we cover this with closure in separate notes

Nested Functions A related topic to first class functions is nested functions, again we explore this in more detail when we look at closure, but let’s consider the following three examples In Pascal In F# C does not permit nested functions, the best we can do is as follows function outer(x : integer): integer; function inner(y : integer): integer; begin inner := x + y end; outer :=inner(x + 1) let outer x = let inner y = x + y inner (x + 1) int inner(int x, int y) {// notice we have to pass two parameters to inner return x + y; } int outer(int x) { // unlike in the other two languages (int)(*ptr)(int, int) = &inner; int temp = (*ptr)(x+1); }

ML Static scoped Strongly typed (unlike LISP which was typeless) Functions and params can be typed, otherwise uses type inferencing function parameters use polymorphism when types are of data structures (e.g., objects, strings, types of arrays) Side effects in functions are permitted as there are no true variables (only params and constants), side effects can only occur via pointers to values and to data structures No variables, identifiers are function names, parameters and constants Automatic memory management (allocation and deallocation of heap- based structures)

ML Functions Functions defined using fun name(params) = expression multiple expressions are permitted if separated by ; in which case the function returns the last expression evaluated the parameter and return value must be typed if it cannot be inferred fun add1(x : int) : int = x + 1; needed here because x could be any numeric type being a functional language, functions should operate only on parameters (we’ll see in a couple of slides how to use local variables) functions are generally going to be recursive which have the following form fun fact(0) = 1 | fact(1) = 1 | fact(n : int) : int = n * fact(n – 1);

Control Statements As seen in the last slide, one form of condition is in a function where we compare the parameter to a (or multiple) value(s) and use | to list options There is also an if-then-else statement there is no if-then statement because all instructions in ML must return a value and if the condition is false, there would be nothing for the if-then to return the return value of the if-then-else is the last statement evaluated in the then or else clause if x < y then 0 else x * (y – 1); The while loop has the form while condition do statement(s); being a functional language, using the while loop is generally discouraged

Local Variables As ML is a functional language, the intent is to only have parameters in your functions, but local variables are available All variables are reference variables to declare a variable, use val variablename to create storage for the variable, use ref initialvalue example: val x = ref 0; You must dereference your variable when obtaining its value (but not when setting it) x := !x + 1; Below is an example of computing the nth Fibonacci value val n1 = ref 1 and n2 = ref 1 and n3 = ref 0 and count = ref 2; while !count < !n do ( n3 = $n1; n1 = $n1 + $n2; n2 = $n3; count = $count + 1)

Data Structure Lists are denoted as [ ] with values separated by commas as in [ 1, 3, 5, 7, 9 ] or [ “apple”, “banana”, “cherry”, “date” ] sublists can be denoted using [ [ … ], [ … ], …, [ … ] ] [ ] is an empty list List operators include List.length :: - like LISP’s cons operator if x is the first list above, then 0 :: x gives us [ 0, 1, 3, 5, 7, 9 ] @ - append if x is the first list above, then x @ 0 gives us [ 1, 3, 5, 7, 9, 0 ] Tuples are denoted using ( ) You can access a particular element from a list or tuple using the following notation [a::b] – gives you access to the head (a) and the rest (b) (_, b) – gives you access to the second item in the tuple

Example Code Function to convert a verb into its past tense fun past “run” = “ran” | past “swim” = “swum” | past x = x ^ “ed” // ^ is append for strings Function which receives a list of integers and sums them together fun sum nil = 0 // base case, empty list return 0 | sum(h::t) = h + sum t // return head of list + pass rest of list to sum Function to reverse a list fun reverse nil = [ ] | reverse (x::xs) = reverse(xs) @ [x]

Haskell We omit coverage of the Haskell syntax Very much like ML with three differences functions can be overloaded pure functional language (no side effects allowed on any data structure) non-strict syntax is allowed a non-strict language is one in which actual parameters do not have to be evaluated prior to the function call taking place this is handled through lazy evaluation this may be more efficient for instance if a particular parameter is only used in some occasions (e.g., in an else clause) and so its evaluation may not be needed it also permits “infinite” lists (lists which are semantically infinite although in practice of course there is a finite storage space) We omit coverage of the Haskell syntax

Functional and Imperative Support Many functional languages now include imperative features LISP for instance has loops, goto statements, local variables ML although it has no local variables, has data structures and permits side effects F# is actually a combination of functional, OO and procedural language Many imperative languages are now being developed to include functional components including first-class functions and closure F# C# Java 8 Ruby Python JavaScript

Other Strengths of Functional Programming Should make parallel processing and multithreading easier since the execution of functions should never interact with each other (no side effects) and so we can more easily distribute code onto multiple processors Can select lazy or eager evaluation lazy evaluation, with memoization, should be more efficient and so we might prefer this, but either implementation should give us the same result Most FPs that offer data structures handle allocation/deallocation for you while this is also true of many imperative languages today, it is not true of all imperative languages, predominantly C/C++