Functional Programming

Similar documents
FP Foundations, Scheme

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

Evaluating Scheme Expressions

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

CS 314 Principles of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

Project 2: Scheme Interpreter

Functional Programming - 2. Higher Order Functions

CS 314 Principles of Programming Languages

Introduction to Functional Programming

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

Lambda Calculus see notes on Lambda Calculus

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

CS 360 Programming Languages Interpreters

CS 314 Principles of Programming Languages

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Functional Programming. Pure Functional Programming

Functional Programming Lecture 1: Introduction

Turtles All The Way Down

User-defined Functions. Conditional Expressions in Scheme

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

Introduction to Scheme

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Functional Languages. Hwansoo Han

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

Functional Programming

Organization of Programming Languages CS3200/5200N. Lecture 11

CS 415 Midterm Exam Spring 2002

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

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

1.3. Conditional expressions To express case distinctions like

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Functional Programming. Contents

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

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

Lexical vs. Dynamic Scope

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

A Brief Introduction to Common Lisp

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

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

Comp 311: Sample Midterm Examination

Programming Systems in Artificial Intelligence Functional Programming

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

Scheme: Expressions & Procedures

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

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Lambda Calculus. Gunnar Gotshalks LC-1

SCHEME AND CALCULATOR 5b

Functional Programming Languages (FPL)

Robot Programming with Lisp

Chapter 15. Functional Programming Languages

Equivalent Notations. Higher-Order Functions. (define (f x y) ( body )) = (define f (lambda (x y) ) ) Anonymous Functions.

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 360 Programming Languages Day 14 Closure Idioms

A Brief Introduction to Scheme (I)

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Chapter 11 :: Functional Languages

Functional Java Lambda Expressions

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

Presented By Andrew Butt

CSc 520 Principles of Programming Languages

Documentation for LISP in BASIC

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

A Small Interpreted Language

Scheme: Strings Scheme: I/O

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

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

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

CS558 Programming Languages

Chapter 15 Functional Programming Languages

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Chapter 15. Functional Programming Languages

Common LISP Tutorial 1 (Basic)

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

Functional programming in LISP

Lambda Calculus LC-1

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Scheme Quick Reference

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

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

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

The PCAT Programming Language Reference Manual

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

LISP Programming. (23 (this is easy) hello 821)

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Control in Sequential Languages

LECTURE 16. Functional Programming

CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College. Scheme

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Scheme Quick Reference

CSc 520 Principles of Programming Languages

SOFTWARE ARCHITECTURE 6. LISP

CS 275 Name Final Exam Solutions December 16, 2016

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

An Explicit Continuation Evaluator for Scheme

Transcription:

Functional Programming CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

When the Function is the Thing In O-O programming, you typically know where an action is needed, but what is to be done depends on the particulars. In functional programming, you typically know what action is needed, but where it is to be done depends on the particulars. Some programming languages make passing functions around and combining them easy. These are known as functional programming languages.

Functional Programming Some believe the need to use concurrency for future hardware speed-up as the end of the free lunch and see FP as the solution. Advocates say If you aren t programming functionally, you are programming dysfunctionally FP here stands for functional programming, but is also the name of a particular functional programming language by John Backus, of Fortran fame.

Related Concepts All functional programming languages allow you to pass functions as parameters, return them as results, construct new functions by composing others, etc. Some do not allow variable update or structure modification. Some have lazy evaluation. When you do this some things get easier and some things get harder.

A Language with a Functional Subset Scheme is a multi-paradigm programming language in the Lisp family with a nice functional subset. Developed in the 1970s by Guy Steele and his PhD Supervisor Gerald Sussman at MIT. Used as a first language of instruction at MIT in the pre-java era. Used as a first language of instruction at Waterloo in the post-java era.

Java Man Pithecanthropus erectus from The Outline of Science J. Arthur Thomson (1922)

Elements of Scheme Syntax: (operator arg...) Some operators are built-in, others programmer defined. lambda: create a function (lambda (n) (+ n 1)) if: conditional evaluation (if (> n 0) n (- n)) define: introduce a name (valid at top level and certain other places) (define n 7) (define factorial (lambda (n) (if (= n 1) 1 (* n (factorial (- n 1)))) )

List Operations (cons a b) create a pair data structure (car p) (cdr p) first element of a pair second element of a pair (null? x) ( ) test whether x is a null pointer. special syntax for the null pointer. (list a1...) short-hand for some cons-es ending with null. (cons 1 (cons 2 (cons 3 (cons 4 ( ) )))) (list 1 2 3 4)

Recursive Structures With recursive list data structures, it is natural to write recursive programs. Make a new list by adding 3 to each element of an input list: (define add-3-to-each (lambda (l) (if (null? l) () (cons (+ 3 (car l)) (add-3-to-each (cdr l)) ) ) )) Make a new list by squaring each element of an input list: (define square-each (lambda (l) (if (null? l) () (cons (* (car l) (car l)) (square-each (cdr l)) ) ) ))

Functions Can Be Arguments (define call-my-function (lambda (f a) (f a))) (define call-fun-on-list-elements (lambda (f l) (if (null? l) () (cons (f (car l)) (call-fun-on-list-elements f (cdr l)) ) ) ) (define zipper (lambda (f l1 l2) (if (or (null? l1) (null? l2)) () (cons (f (car l1) (car l2)) (zipper f (cdr l1) (cdr l2)) ) ) ))

Local Bindings Local variables may be introduced with let It has the form (let ( (var1 initial-value1) (var2 initial-value2)...) expr1 expr2...) E.g. (define factorial (lambda (n) (let ((nm1 (- n 1))) (if (< n 2) 1 (* n (factorial nm1)) ) ) ))

Lexical Scoping An inner function use all the local names of the functions that enclose it. (define outer-fn (lambda (n) (let ( (inner-fn (lambda (m) (+ m n)) ) ) (inner-fn (+ n 2) ) ))

Returning Functions: Closures E.g. (define add (lambda (a) (lambda (b) (+ a b)) )) What is the value of a when the inner function is returned? It is the value of a that add was called with. E.g. (add 3) => (lambda (b) (+ a b)) ; with a = 3

Returning Functions: Closures E.g. A counter... (define make-counter (lambda () (let ((count 0)) (lambda (n) (set! count (+ count n)) count ) ) )) (define counter1 (make-counter)) (counter1 7) ; yields 7 (counter1 8) ; yields 15 (define counter2 (make-counter)) (counter2 9) ; yields 9 (counter2 3) ; yields 12 (counter1 3) ; yields 18

Functional Programming Tricks Functional composition (define compose (lambda (f g) (lambda (a) (f (g a))) )) E.g. (define negative-inverse (compose - /)) (negative-inverse 9) ; Yields 1/9

Functional Programming Tricks Convert make a unary function from a binary function: (define curry (lambda (f) (lambda (a) (lambda (b) (f a b)) (define plus (curry +)) (define plus5 (plus 5)) (define nine (plus5 4)) ((plus 5) 4) ; Yields 9

Functional Programming Tricks Changing the order of arguments: (define twist (lambda (f) (lambda (a b) (f b a)) )) (define subtract-from (twist -)) (subtract-from 9 11) ; Yields 2 (define minus1 ((curry subtract-from) 1)) (minus1 9) ; Yields 8

Composing Functional Elements Very powerful Complex ideas can be expressed with short programs Be careful not to write unreadable code.

Functional Programming with Lists map: (map f (list a b c d)) gives (list (f a) (f b) (f c) (f d)) This is built in in Scheme.

Functional Programming with Lists reduce: (reduce f (list a b c d)) gives (f a (f b (f c d))) E.g. (reduce + (list 1 2 3 4 5)) ; Yields 15 (define dot-product (lambda (u v) (reduce + (zipper * u v)) )) (define eval-line (lambda (x) (lambda (b a) (+ b (* a x)) ))) (define eval-poly (lambda (x) (lambda (l) (reduce (eval-line x) l)))) ((eval-poly 2) (list 5 4 3 2 1)) ; Yields 57

Functional Programming with Lists Spread: (spread (list f g h) x) gives (list (f x) (g x) (h x)) Question: Write the spread function using list operations. Question: Write the spread function using map and lambda.

Lazy Evaluation: Force and Delay delay creates a promise... An object that may be evaluated later. force causes the promise to be evaluated to give a value. E.g. (define make-five (lambda () (write Hello ) (+ 2 3))) (define five (delay (make-five))) ; make-five not called y...... (define fiveno (force five)) ; make-five called here.