Chapter 15 Functional Programming Languages

Similar documents
Chapter 15. Functional Programming Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

Chapter 15. Functional Programming Languages ISBN

Organization of Programming Languages CS3200/5200N. Lecture 11

FP Foundations, Scheme

Chapter 15. Functional Programming Languages

Example Scheme Function: equal

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

Functional Programming Languages (FPL)

LECTURE 16. Functional Programming

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

15. Functional Programming

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

Documentation for LISP in BASIC

CS 314 Principles of Programming Languages

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

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

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

Scheme Quick Reference

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

Functional Programming

Functional Programming. Pure Functional Languages

Scheme Quick Reference

Functional Programming. Pure Functional Languages

User-defined Functions. Conditional Expressions in Scheme

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

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

Introduction to Functional Programming

Concepts of Programming Languages

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

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

CSC 533: Programming Languages. Spring 2015

An introduction to Scheme

Functional Programming. Pure Functional Programming

Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Functional Programming Languages (FPL)

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

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

CSE 341 Section Handout #6 Cheat Sheet

Lisp. Versions of LISP

Introductory Scheme. Revision 1

Functional Programming - 2. Higher Order Functions

A Brief Introduction to Scheme (I)

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Principles of Programming Languages

CS 314 Principles of Programming Languages

Func%onal Programming in Scheme and Lisp

Chapter 1. Fundamentals of Higher Order Programming

Scheme: Expressions & Procedures

Imperative, OO and Functional Languages A C program is

Func%onal Programming in Scheme and Lisp

Programming Systems in Artificial Intelligence Functional Programming

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

Functional Programming Lecture 1: Introduction

Imperative languages

Functional programming in LISP

CSc 520 Principles of Programming Languages

An Introduction to Scheme

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

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. Lecture 16

Introduction to Scheme

SCHEME AND CALCULATOR 5b

Principles of Programming Languages 2017W, Functional Programming

Functional programming with Common Lisp

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

A Brief Introduction to Scheme (II)

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?

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

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

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

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

Functional Languages. Hwansoo Han

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

COP4020 Programming Assignment 1 - Spring 2011

Streams and Evalutation Strategies

Functional Programming. Contents

1.3. Conditional expressions To express case distinctions like

Administrivia. Simple data types

Principles of Programming Languages

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

Lambda Calculus see notes on Lambda Calculus

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

6.001: Structure and Interpretation of Computer Programs

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

Common LISP Tutorial 1 (Basic)

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

6.001 Notes: Section 8.1

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

Scheme Basics > (butfirst '(help!)) ()

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

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

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

Typed Scheme: Scheme with Static Types

Scheme: Strings Scheme: I/O

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

Transcription:

Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions. It emphasizes the application of functions, in contrast with the imperative programming style that emphasizes changes in state. Thus, programs are collections of mathematical function definitions and function applications. It emphasized in academia rather than in commercial software development. Languages used in commercial applications include Mathematica (symbolic math), Haskell, ML (financial analysis), and XSLT. It includes APL, Haskell, Lisp, ML, Scheme, XSLT, etc. 1

What is a mathematical function? A rule that associates members of one set (the domain) with members of another set. cube(x) x * x * x Early theoretical work on mathematical functions separated the task of defining a function from that of naming the function. Lambda expression provides a method for defining nameless functions. Lambda Expressions Syntax: LAMBDA (parameters) body LAMBDA(x) x * x * x for the function cube (x) x * x * x 2

Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression e.g., (LAMBDA(x) x * x * x)(2) which evaluates to 8 Higher-order functions A higher-order function is one that either takes functions as parameters or yields a function as its result, or both. (1) Function 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 f(x) x + 2 and g(x) 3 * x, h(x) f(g( x)) (3 * x)+ 2 3

(2) Apply-to-all: Higher-order function 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 Apply-to-all is denoted by α h(x) x * x α(h,(2,3,4)) yields (4,9,16) 4

Fundamentals of Functional Programming Languages The basic process of computation is fundamentally different in a Functional Programming Language than in an imperative language 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 - Functional Programming Language, variables are not necessary, as is the case in mathematics. LISP Data Types and Structures The most widely used. Two types of data objects in LISP: Atoms and lists Atoms: either symbols, in the form of identifiers or they are numeric literals List: parenthesized collections of sublists and/or atoms e.g., (A B (C D) E) 5

Scheme A dialect of LISP, designed to be a cleaner, more modern, and simpler version than the contemporary dialects of LISP Scheme includes primitive functions for the basic arithmetic operations. Primitive Functions Arithmetic: +, -, *, /, ABS, SQRT, REMAINDER, MIN, MAX function calls were specified in a prefix list form For example, if + is a function that takes two numeric parameters, (+ 5 2) yields 7 6

Special Form Function: DEFINE DEFINE serves two fundamental needs of Scheme programming 1. Bind a symbol to an expression e.g., (DEFINE pi 3.141593) (DEFINE two_pi (* 2 pi)) 2. Bind names to expressions, DEFINE takes two lists as parameters. The first is the prototype of function call, with the function name followed by the formal parameters, together in a list. bound. The second list includes one or more expressions to which the name is to be The general form of DEFINE: (DEFINE (function_name parameters) expression {expression} ) e.g., (DEFINE (square x)(* x x)) Example use: (square 5) 7

Example: Compute the length of the hypotenuse of a right triangle, given the lengths of the two other sides (DEFINE (hypotenuse side1 side2) (SQRT(+(square side1)(square side2))) ) Output Functions (DISPLAY expression) (NEWLINE) Numeric Predicate Functions A predicate function is one that returns a Boolean value #T is true and ()is false 8

Function Meaning = Equal <> Not Equal > Greater than < less than EVEN? Is it an even number? ODD? Is it an odd number? ZERO? Is it zero? NEGATIVE? Is it a negative number? Control Flow: IF Scheme has two control structure one for two-way selection and one for multiple selection. Two-way Selection (IF predicate then_exp else_exp) e.g., (IF (<> count 0) (/ sum count) 0) 9

Another Example (factorial function) (DEFINE (factorial n) (IF (= n 0) 1 (* n (factorial(- n 1))) )) Control Flow: COND Multiple Selection General form: (COND (predicate_1 expr {expr}) (predicate_2 expr {expr})... (predicate_n expr {expr}) (ELSE expr {expr}) ) The predicates of the parameters are evaluated one at a time, in order from the first until one evaluates to #T. 10

Returns the value of the last expr in the first pair whose predicate evaluates to true In some implementations, ELSE is optional. Example of COND (DEFINE (compare x y) (COND ((> x y) (DISPLAY x is greater than y )) ) ((< x y) (DISPLAY y is greater than x )) (ELSE (DISPLAY x and y are equal )) ) 11

List Functions: The most common use of the Scheme is list processing. List functions deal with lists. The syntax to define a list is: '(a b c) where a, b, and c are constants. We use the apostrophe (') to indicate that what follows in the parentheses is a list of constant values, rather than a function or expression. An empty list can be defined as such: '() or simply: () 12

List Functions: CONS CONS is a primitive list constructor. It builds a list from its two arguments. CONS takes two parameters, the first of which can be either an atom or a list and the second of which is a list; returns a new list that includes the first parameter as its first element and the second parameter as the remainder of its result e.g., (CONS 'A '(B C)) returns (A B C) List Functions: CAR and CDR CAR takes a list parameter; returns the first element of that list e.g., (CAR '(A B C)) yields A (CAR '((A B) C D)) yields (A B) CDR takes a list parameter; returns the list after removing its first element e.g., (CDR '(A B C)) yields (B C) (CDR '((A B) C D)) yields (C D) 13

Predicate Function: EQ? EQ? takes two symbolic parameters; it returns #T if both parameters are atoms and the two are the same e.g., (EQ? 'A 'A) yields #T (EQ? 'A 'B) yields () Predicate Functions: LIST? and NULL? LIST? takes one parameter; it returns #T if the parameter is a list; otherwise() NULL? takes one parameter; it returns #T if the parameter is the empty list; otherwise() 14

Example Scheme Function: member member takes an atom and a simple list; returns #T if the atom is in the list; () otherwise DEFINE (member atm lis) (COND ((NULL? lis) '()) ((EQ? atm (CAR lis)) #T) ((ELSE (member atm (CDR lis))) )) Example Scheme Function: equalsimp //CAR returns the first element of that list //CDR returns the list after removing its first element equalsimp takes two simple lists as parameters; returns #T if the two simple lists are equal; () otherwise (DEFINE (equalsimp lis1 lis2) (COND ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) '()) ((EQ? (CAR lis1) (CAR lis2)) (equalsimp(cdr lis1)(cdr lis2))) (ELSE '()) )) 15

Scheme Function: LET A function allows names to be temporarily bound to the values of expressions. These names can then be used in the evaluation of another expression. General form: (LET ( (name_1 expression_1) (name_2 expression_2)... (name_n expression_n)) body ) Evaluate all expressions, then bind the values to the names; evaluate the body 16

LET Example (DEFINE (quadratic_roots a b c) (LET ( (root_part_over_2a (/(SQRT(-(* b b)(* 4 a c)))(* 2 a))) (minus_b_over_2a (/(- 0 b)(* 2 a))) (DISPLAY (+ minus_b_over_2a root_part_over_2a)) (NEWLINE) (DISPLAY (- minus_b_over_2a root_part_over_2a)) )) 17