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

Similar documents
Lambda Calculus see notes on Lambda Calculus

Functional Programming. Contents

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

Lisp Basic Example Test Questions

Functional Programming. Big Picture. Design of Programming Languages

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Functional programming with Common Lisp

CSCI 334: Principles of Programming Languages. Lecture 2: Lisp Wrapup & Fundamentals. Higher-Order Functions. Activity

Lecture Notes on Lisp A Brief Introduction

Introduction. chapter Functions

CSCI337 Organisation of Programming Languages LISP

FP Foundations, Scheme

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

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

John McCarthy IBM 704

Common Lisp. Blake McBride

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

UMBC CMSC 331 Final Exam

Introduction to Functional Programming and basic Lisp

Functional Programming

Announcements. Today s Menu

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

User-defined Functions. Conditional Expressions in Scheme

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Functional Languages. Hwansoo Han

Associative Database Managment WIlensky Chapter 22

Associative Database Managment

PL Revision overview

CS A331 Programming Language Concepts

Functional Programming

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

UMBC CMSC 331 Final Exam

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

Chapter 15. Functional Programming Languages

Lisp. Versions of LISP

Func%onal Programming in Scheme and Lisp

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Func%onal Programming in Scheme and Lisp

Chapter 1. Fundamentals of Higher Order Programming

Imperative languages

by the evening of Tuesday, Feb 6

CS 314 Principles of Programming Languages

Lecture #2 Kenneth W. Flynn RPI CS

A Small Interpreted Language

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

Putting the fun in functional programming

Functional Programming. Pure Functional Languages

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

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

4. Functional Programming Language-Oriented Programming

Midterm Examination (Sample Solutions), Cmput 325

Debugging in LISP. trace causes a trace to be printed for a function when it is called

Example Scheme Function: equal

Lists. Michael P. Fourman. February 2, 2010

Robot Programming with Lisp

Presented By Andrew Butt

Functional Programming Languages (FPL)

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS 314 Principles of Programming Languages

comparing Lisp and Prolog

MIDTERM EXAMINATION - CS130 - Spring 2005

CSCI-GA Scripting Languages

CMSC 331 Final Exam Section 0201 December 18, 2000

Chapter 15 Functional Programming Languages

INF4820. Common Lisp: Closures and Macros

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

A little bit of Lisp

Review of Functional Programming

Lisp History. Lisp and Prolog Background. Lisp Use. Lisp Availability

Introduction 2 Lisp Part III

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

UMBC CMSC 331 Final Exam Section 0101 December 17, 2002

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

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

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

An Explicit-Continuation Metacircular Evaluator

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

CMSC 331 Final Exam Fall 2013

Functional Programming - 2. Higher Order Functions

Programming Languages

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

Basics of Using Lisp! Gunnar Gotshalks! BLU-1

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

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

SOFTWARE ARCHITECTURE 6. LISP

Software System Design and Implementation

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

CSE413 Midterm. Question Max Points Total 100

low and not larger than high. 18 points

Imperative, OO and Functional Languages A C program is

CS558 Programming Languages

Introduction to Functional Programming

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4

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

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

Scheme: Strings Scheme: I/O

19 Machine Learning in Lisp

LISP. Introduction (some words about history...) Theoretical Assumptions Bridging the Gap Overview of Common Lisp Lisp versus ML Conclusion

Transcription:

History Functional Programming Based on Prof. Gotshalks notes on functionals 1977 Turing 1 Lecture John Backus described functional programming The problem with current languages is that they are word-at-a-time 2 > Notable exceptions then were Lisp and APL > Now ML 1 Turing award is the Nobel prize of computer science. 2 'Word-at-a-time' translates to 'byte-at-a-time' in modern jargon. A word typically held 2 to 8 bytes depending upon the type of computer. F2-1 F2-2 Meaningful Units of Work Work with operarations meaningful to the application, not to the underlying hardware & software» Analogy with word processing is not to work with characters and arrays or lists of characters» But work with words, paragraphs, sections, chapters and even books at a time, as appropriate. FP form Note the following properties of functional programs» NO explicit loops ( or recursion)» NO sequencing at a low level» NO local variables In addition, functional programs have the following properties» functions as input» functions as output > In FP frequently write functions that produce a new function using other functions as input F2-3 F2-4

Library of functions Depending upon the application area functions are created.» For example trans transpose a matrix Some are created using existing functionals» For example innerproduct Library of functions 2 Others are created outside of the system for efficiency reasons» For example trans may be more efficient to implement outside of Lisp Although as compiler knowledge grows compilers produce more efficient code than 'coding by hand' Machine speeds increase so many functions execute fast enough The files prism:/cs/course/functionals.lsp and prism:/cs/course/functionalsbase.lsp created by Prof. Gotshalks contain additional library functions F2-5 F2-6 Binding function - bu - 1 Given a binary function it is often useful to bind the first parameter to a constant creating a unary function > Also called currying after the mathematician Curry who developed the idea» (bu + 3) creates a unary add 3 from the binary function + (mapcar (bu + 3) (1 2 3)) ==> (4 5 6)» Cons x before every item in a list (mapcar (bu cons x) (1 2 3)) ==> ((x.1) (x.2) (x.3))» Note that mapcar expects a function definition as the second argument, so we use bu to help construct the function Binding function - bu - 2 We define the function bu as follows: (defun bu ( f x ) (function (lambda ( y ) ( funcall f x y ) ) ) ) F2-7 F2-8

Binding function - bu - 3 We could define the function 3+ ( define 3+ ( x ) ( + 3 x ) )» and use (mapcar 3+ (1 2 3)) ==> (4 5 6)» but this adds to our name space For use-once functions we can use lambda expressions (mapcar # (lambda (x) (+ 3 x)) (1 2 3)) ==> (4 5 6) (mapcar (function ( lambda (x) (+ 3 x) ) ) (1 2 3)) ==> (4 5 6) Binding function - bu - 4 The previous slide solutions are seen as being clumsy and more difficult to read compared to the following bu has a clear meaning with the above you have to reverse engineer to understand (mapcar (bu + 3) (1 2 3)) ==> (4 5 6) Can define functions using bu (defun 3+ (y) (funcall (bu + 3) y)) In such cases we would write (defun 3+ (y) (+ 3 y)) We do not normally use bu to define named functions F2-9 F2-10 The Functional rev rev reverse the order of the arguments of a binary function (defun rev (f) # (lambda (x y) (funcall f y x)) ) Earlier we wrote (mapcar (bu cons x) (1 2 3)) ==> ((x.1) (x.2) (x.3)) Suppose we want ((1.x) (2.x) (3.x)) then we write (mapcar (bu (rev cons) x) (1 2 3)) ==> ((1.x) (2.x) (3.x)) Other Functionals in the file - 1 In prism:/cs/course/3401/functionals.lsp the following functionals are described (comp unaryfunction1 unaryfunction2) > Compose two unary functions (compl unaryfunction1 unaryfunction2... unaryfunctionn) > Compose a list of unary functions (trans matrix) > Transpose a matrix F2-11 F2-12

Other Functionals in the files - 2 (distl anitem thelist) > Distribute anitem to the left of items in thelist (distl a (1 2 3)) ==> ((a 1) (a 2) (a 3)) (distr anitem thelist) > Distribute anitem to the right of items in thelist (distr a (1 2 3)) ==> ((1 a) (2 a) (3 a)) functional examples > (mapcar ( bu 'member 'y ) '( ( y ) ( ) ( a b) ( y x ) ) ) ( ( Y ) NIL NIL ( Y X ) ) filter - filters a list keeping items that satisfy a predicate > ( filter 'atom '( a () (a b) (x) c ) ) ( A NIL C ) There are many MANY more functions in both functionals.lsp and functionalsbase.lsp look at your leisure!! comp - defines a function as being the composition of a pair of unary functions > ( funcall (comp 'sqrt 'abs ) -9 ) 3.0 F2-13 F2-14 Another functional example - 1 Another functional example - 2 Problem: > ( allpairs (1 2) (x) ) Write a fully functional (no recursion) function, allpairs, ( ( 1 x ) ( 2 x ) ) that given two lists, list1 and list2, returns a list of lists. Each list has 2 elements, 1 from list1 and 1 from list2. All possible pairs are included. ( ( A 1 ) ( B 1 ) ( C 1 ) ( A 2 ) ( B 2 ) ( C 2 ) ( A 3 ) ( B 3 ) ( C 3 ) ) F2-15 F2-16

Another functional example - 3 Another functional example - 4 From functionals.lsp: Distribute right distributes the first parameter to the right of Each of the elements in the second parameter. distr - functional 2 parameter version distr1 functional 1 parameter version > (distr a ( 1 2 3 ) ) ( ( 1 A ) ( 2 A ) ( 3 A ) ) > (distr1 (a ( 1 2 3 ) ) ) ( ( 1 A ) ( 2 A ) ( 3 A ) ) (???? ) F2-17 F2-18 Another functional example - 5 Another functional example - 6 ( distr1 ( list a b ) ) ) ( mapcar distr1 (distr1 ( list a b ) ) ) ) ( ( 1 ( A B C ) ) ( 2 ( A B C ) ) ( 3 ( A B C ) ) ) ( ( ( A 1 ) ( B 1 ) ( C 1 ) ( ( A 2 ) ( B 2 ) ( C 2 ) ) ( ( A 3 ) ( B 3 ) ( C 3 ) ) ) F2-19 F2-20

Another functional example - 7 Yet another functional example - 1 ( apply 'append ( mapcar 'distr1 (distr1 ( list a b ) ) ) ) Generate the list (1 3 9 27 81). ) ( ( A 1 ) ( B 1 ) ( C 1 ) ( A 2 ) ( B 2 ) ( C 2 ) ( A 3 ) ( B 3 ) ( C 3 ) ) F2-21 F2-22 Yet another functional example - 2 Yet another functional example - 3 Functions for list generation ( genlist length next start ) : generate a list, start is the first item in the list, length is the length of the list and next is the one parameter function that generates the second item. ( range lower upper ) : range gives a list of integers in the range lower to upper inclusive ( genlist 5???? 1 ) Math function ( sigma lower upper func ) : add a function of the integers between lower and upper inclusive F2-23 F2-24

Yet another functional example - 4 ( genlist 5 (bu * 3 ) 1 ) F2-25