Functional Programming. Pure Functional Languages

Similar documents
Functional Programming. Pure Functional Languages

CS 314 Principles of Programming Languages

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

Functional Programming

Functional Programming. Pure Functional Programming

Organization of Programming Languages CS3200/5200N. Lecture 11

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

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

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

Functional Programming. Big Picture. Design of Programming Languages

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

CS 314 Principles of Programming Languages. Lecture 16

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

Functional Programming - 2. Higher Order Functions

Scheme: Expressions & Procedures

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

Functional Languages. Hwansoo Han

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

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

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

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

Introduction to Scheme

FP Foundations, Scheme

Documentation for LISP in BASIC

CS 314 Principles of Programming Languages

Principles of Programming Languages 2017W, Functional Programming

User-defined Functions. Conditional Expressions in Scheme

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

A Small Interpreted Language

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

Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Lisp. Versions of LISP

CS 314 Principles of Programming Languages

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

CS 314 Principles of Programming Languages

Lambda Calculus. Gunnar Gotshalks LC-1

CSc 520 Principles of Programming Languages

LECTURE 16. Functional Programming

Scheme Quick Reference

Functional programming with Common Lisp

Chapter 15. Functional Programming Languages

CSCC24 Functional Programming Scheme Part 2

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

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

CSC 533: Programming Languages. Spring 2015

Scheme Quick Reference

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?

Lambda Calculus LC-1

Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

ALISP interpreter in Awk

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

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

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Introduction to Functional Programming

CSCI337 Organisation of Programming Languages LISP

An Introduction to Scheme

Project 2: Scheme Interpreter

A Brief Introduction to Scheme (II)

Imperative, OO and Functional Languages A C program is

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

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

Imperative languages

1.3. Conditional expressions To express case distinctions like

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

CS 360 Programming Languages Interpreters

Lisp Basic Example Test Questions

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Chapter 15 Functional Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

CS61A Midterm 2 Review (v1.1)

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

Lecture #24: Programming Languages and Programs

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

An introduction to Scheme

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

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Functional programming in LISP

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

COP4020 Programming Assignment 1 - Spring 2011

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

A Brief Introduction to Scheme (I)

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Functional Programming Lecture 1: Introduction

Chapter 1. Fundamentals of Higher Order Programming

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

Evaluating Scheme Expressions

Programming Languages

15 Unification and Embedded Languages in Lisp

Scheme: Strings Scheme: I/O

SCHEME AND CALCULATOR 5b

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Lecture Notes on Lisp A Brief Introduction

Principles of Programming Languages

CS 275 Name Final Exam Solutions December 16, 2016

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Transcription:

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure Functional Languages Referential transparency value of an expression is independent of context where the function application occurs means that all variables in a function body must be local to that function; why? There is no concept of assignment variables are bound to values only through parameter associations no side effects 2 1

Pure Functional Languages Control flow accomplished through function application (and recursion) a program is a set of function definitions and their application to arguments Implicit storage management copy semantics, needs garbage collection Functions are 1st class values! can be returned as value of an expression or function application can be passed as an argument can be put into a data structure and saved Unnamed functions exist as values 3 Pure Functional Languages Lisp designed for symbolic computing simple syntax data and programs have same syntactic form S-expression function application written in prefix form (e1 e2 e3 ek) means Evaluate e1 to a function value Evaluate each of e2,,ek to values Apply the function to these values (+ 1 3) evaluates to 4 4 2

History Lisp 1950 s John McCarthy dynamic scoping Scheme 1975 Guy Steele Gerald Sussman Common Lisp standardized PL lexical scoping functions as first class values continuations Racket is a PL based on Scheme; Scheme has eager evaluation and dynamic typing 5 S-expressions S-expr ::= Name Number ( { S-expr } ) Name is a symbolic constant, some string of chars which starts off with anything that can t start a Number Number is an integer or real number E.g., (a (b c ) (d)) is an S-expr (or list) car selects the first element car of this S-expr is a cdr selects the rest of the list cdr of this S-expr is ((b c) (d)) a b c ( ) d ( ) ( ) 6 3

List Operators Car and cdr Given a list, they decompose it into first element, rest of list portions Cons Given an element and a list, cons builds a new list with the element as its car and the list as its cdr () means the empty list in Scheme History: the words car and cdr come from the first implementation of Lisp on the IBM 704. car stands for Contents of the Address part of Register cdr stands for Contents of the Decrement part of the Register and the implementation represented lists so that the head was a car and the tail was a cdr 7 Examples (car (a b c)) is a (car ((a) b (c d))) is (a) (cdr (a b c)) is (b c) (cdr ((a) b (c d))) is (b (c d)) a () ((a) b (c d)) Can compose these operators in a short-hand manner. Can reach any arbitrary list element by composition of car s and cdr s. (car (cdr (cdr ((a) b (c d))))) = can also be written (caddr ((a) b (c d))) (car (cdr ( b (c d))) = (car ((c d )) = (c d). b c d () () 8 4

Examples (cons (a b c) ((a) b (c d))) is ((a b c) (a) b (c d)) (cons d (e)) is (d e) (cons (a b) (c d)) is ((a b) c d) Useful predicates in Scheme. Note the quote prevents evaluation of the argument as an S-expr. (symbol? sam) returns #true (symbol? 1) returns #f (number? sam) returns #false (number? 1) returns #t (list? (a b)) returns #true (list? a) returns #f (null? ()) returns #true (null? (a b)) returns #f (zero? 0) returns #true (zero? 1) returns #f Can compose these. (zero? (- 3 3)) returns #true --note that since this language is fully parenthesized, there are no precedence problems in the expressions! 9 Scheme Fcn-def ::= (define (Fcn-name Param {Param}) S-expr) Fcn-name should be a new name for a fcn. Param should be variable(s) that appear in the S-expr which is the function body. Fcn-def ::= ( define Fcn-name Fcn-value) Fcn-value::= (lambda ( Param {Param} ) S-expr) where Param variables are expected to appear in the S-expr; called a lambda expression. 10 5

Scheme Examples ( define (zerocheck? x) (if (= x 0) #true #false) ) If-expr ::= (if S-expr0 S-expr1 S-expr2) where S-expr0 must evaluate to a boolean value; if that value is true, then the If-expr returns the value of S-expr1, else the value of S-expr2. (zerocheck? 1) returns #false, (zerocheck? (* 1 0)) returns #true (define (notnumber x) (not (number? x))) Where number? returns #true if its argument is a number and #false otherwise; note not is a logical operator 11 Scheme Examples (define square (lambda (n) (* n n)) ) This associates the Fcn-name square with the function value (lambda (n) (* n n))) Lambda calculus is a formal system for defining recursive functions and their properties Set of functions definable using lambda calculus (Church 1941) is same as set of functions computable as Turning Machines (Turing 1930 s) 12 6

Read-eval-print loop How does a Scheme interpreter work? Read input from user A function definition or abstraction A function evaluation Evaluate input Store function definition (e1 e2 e3 ek) Evaluate e1 to obtain a function Evaluate e2,, ek to values Execute function body using values from previous step as formal parameter values Return value of function Print return value 13 Conditional Execution (if e1 e2 e3) (cond (e1 h1) (e2 h2) (en-1 hn-1) (else hn)) Cond is like a nested if-then-elseif construct (define (zerocheck? x) (cond ((= x 0) #t) (else #f))) OR (define (zchk? x) (cond ((number? x) (zero? x)) (else #f)) ) 14 7

Recursive Functions (define (len x) (cond ((null? x) 0) (else (+ 1 (len (cdr x)))))) (len (1 2)) should yield 2. len is a shallow Trace: (len (1 2)) --top level call recursive function x = (1 2) (len (2)) --recursive call 1 x = (2) (len ( ) ) -- recursive call 2 x = ( ) returns 0 --return for call 2 returns (+ 1 0) =1 --return for call 1 returns (+ 1 1) = 2 --return for top level call (len ((a) b (c d))) returns 3 15 List Append (define (app x y) (cond ((null? x) y) ((null? y) x) (else (cons (car x) (app (cdr x) y))))) (app ( ) ( ) ) yields ( ) (app ( ) ( 1 4 5)) yields (1 4 5) (app (5 9) (a (4) 6)) yields (5 9 a (4) 6) another shallow recursive function Can we write a function that counts the number of atoms in a list? (this will have to be a deep recursive function) 16 8

Atomcount Function atomcount is a deep recursive function (define (atomcount x) (cond ((null? x) 0) ((atom? x) 1) (else (+ (atomcount (car x)) (atomcount (cdr x)))) )) (atomcount (1)) yields 1 (atomcount (1 (2 (3)) (5)) ) yields 4 Trace: (atomcount (1 (2 (3)) ) 1> (+ (atomcount 1) (atomcount ( (2 (3)) ) )) 2> (+ (atomcount (2 (3)) ) (atomcount ( ) ) ) 3> (+ (atomcount 2) (atomcount ((3)) ) etc. 4> (+ (atomcount (3)) (atomcount ( )) ) 1 5> (+ (atomcount 3) (atomcount ( ))) 17 Equality Testing eq? predicate that can check atoms for equal values doesn t work on lists eql? comparison function for lists (define (eql? x y) (or (and (null? x) (null? y)) // null? () is #true (and (atom? x) (atom? y) (eq? x y)) //atom? () is false; checking atoms (and (not (atom? x)) (not (atom? y)) (not (null? x)) (not (null? y))//error checks (eql? (car x) (car y)) (eql? (cdr x) (cdr y))))) 18 9

Examples (eql? (a) (a)) yields #t (eql? a b) yields #f (eql? b b) yields #t (eql? ((a)) (a)) yields #f (eq? a a) yields #t (eq? (a) (a)) yields #f 19 How does eq? work? (define (f x y) (list x y)) Scheme uses a reference model so (f a a) yields (a a). for variables How does Scheme implement this? It binds both x and y to the same atom a. eq? checks that x and y both point to the same place x y a Say we called (f (a) (a)). then x and y don t point to the same list at all! x y a ( ) 20 10