Functional Programming



Similar documents
Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Chapter 15 Functional Programming Languages

CSE 307: Principles of Programming Languages

Lecture 1: Introduction

CSCI 3136 Principles of Programming Languages

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Chapter 7: Functional Programming Languages

1.1 WHAT IS A PROGRAMMING LANGUAGE?

Moving from CS 61A Scheme to CS 61B Java

The countdown problem

VB.NET Programming Fundamentals

CSC Software II: Principles of Programming Languages

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

History OOP languages Year Language 1967 Simula Smalltalk

Functional Programming in C++11

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Programming Languages

How To Program In Scheme (Prolog)

Concepts of Programming Languages: A Unified Approach. Karl Abrahamson

Announcements FORTRAN ALGOL COBOL. Simula & Smalltalk. Programming Languages

Course Goal CMSC 330: Organization of Programming Languages. Studying Programming Languages. All Languages Are (Kind of) Equivalent.

Programming Languages in Artificial Intelligence

[Refer Slide Time: 05:10]

CSE 130 Programming Language Principles & Paradigms

Principles of Programming Languages Topic: Introduction Professor Louis Steinberg

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Semester Review. CSC 301, Fall 2015

Object Oriented Software Design

1998. (R. Bird and P. Wadler, Introduction to Functional Programming, Prentice

Integrating Formal Models into the Programming Languages Course

Chapter 6: Programming Languages

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

On the (un)suitability of Java to be the first programming language

Anatomy of Programming Languages. William R. Cook

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

by Mario Fusco Monadic

7.1 Our Current Model

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Chapter 13 Computer Programs and Programming Languages. Discovering Computers Your Interactive Guide to the Digital World

Introduction to Software Paradigms & Procedural Programming Paradigm

McGraw-Hill The McGraw-Hill Companies, Inc.,

Object Oriented Software Design

Cedalion A Language Oriented Programming Language (Extended Abstract)

How to simplify software development with high level programming languages? Pierre-Alexandre Voye - ontologiae@gmail.com

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

programming languages, programming language standards and compiler validation

Programming Languages CIS 443

Testing and Tracing Lazy Functional Programs using QuickCheck and Hat

Languages september 12, 2015 Éric Lévénez < FORTRAN III end-1958 FORTRAN II FORTRAN I october 1956

1 Operational Semantics for While

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Type Classes with Functional Dependencies

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Object-Oriented Software Specification in Programming Language Design and Implementation

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

1/20/2016 INTRODUCTION

Implementing Programming Languages. Aarne Ranta

ATSBA: Advanced Technologies Supporting Business Areas. Programming with Java. 1 Overview and Introduction

TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT

Parameter passing in LISP

Software Paradigms (Lesson 1) Introduction & Procedural Programming Paradigm

The Cool Reference Manual

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

Chapter 5 Names, Bindings, Type Checking, and Scopes

On Understanding Types, Data Abstraction, and Polymorphism

Ed. v1.0 PROGRAMMING LANGUAGES WORKING PAPER DRAFT PROGRAMMING LANGUAGES. Ed. v1.0

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

The C Programming Language course syllabus associate level

Introduction to Python

Programming Language Concepts for Software Developers

Sources: On the Web: Slides will be available on:

The previous chapter provided a definition of the semantics of a programming

Structure of Presentation. The Role of Programming in Informatics Curricula. Concepts of Informatics 2. Concepts of Informatics 1

The Needle Programming Language

Accentuate the Negative: Homework Examples from ACE

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Multiple Dispatching. Alex Tritthart WS 12/13

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Fundamentals of Programming Languages

Computer Programming I & II*

On Understanding Types, Data Abstraction, and Polymorphism

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

THE ROLE OF PROGRAMMING PARADIGMS IN THE FIRST PROGRAMMING COURSES. 1. Introduction

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Computer Programming I

Programming Languages

CS 241 Data Organization Coding Standards

Form & Function in Software. Richard P. Gabriel phd mfa

Chapter 3. Input and output. 3.1 The System class

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Software Development (CS2500)

C Compiler Targeting the Java Virtual Machine

1 Abstract Data Types Information Hiding

Transcription:

FP 2005 1.1 3 Functional Programming WOLFRAM KAHL kahl@mcmaster.ca Department of Computing and Software McMaster University

FP 2005 1.2 4 What Kinds of Programming Languages are There? Imperative telling the machine what to do Declarative telling the machine what to achieve Programming Languages Imperative Declarative C, Pascal FORTRAN COBOL Modula-2 Object-oriented C++, Oberon-2 Java, Smalltalk Functional Haskell, OCaml ML, Scheme, LISP Logic Prolog Mercury

FP 2005 1.3 5 What Kinds of Programming Languages are There? Imperative telling the machine what to do Declarative telling the machine what to achieve Programming Languages Imperative Declarative C, Pascal FORTRAN COBOL Modula-2 Object-oriented C++, Oberon-2 Java, Smalltalk Functional Haskell, OCaml ML, Scheme, LISP Logic Prolog Mercury

FP 2005 1.4 6 What Kinds of Programming Languages are There? Imperative telling the machine what to do Declarative telling the machine what to achieve Programming Languages Imperative Declarative C, Pascal FORTRAN COBOL Modula-2 Object-oriented C++, Oberon-2 Java, Smalltalk Functional Haskell, OCaml ML, Scheme, LISP Logic Prolog Mercury

FP 2005 1.5 7 What Kinds of Programming Languages are There? Imperative telling the machine what to do Declarative telling the machine what to achieve Programming Languages Imperative Declarative C, Pascal FORTRAN COBOL Modula-2 Object-oriented C++, Oberon-2 Java, Smalltalk Functional Haskell, OCaml ML, Scheme, LISP Logic Prolog Mercury

FP 2005 1.6 8 What Kinds of Programming Languages are There? Imperative telling the machine what to do Declarative telling the machine what to achieve Programming Languages Imperative Declarative C, Pascal FORTRAN COBOL Modula-2 Object-oriented C++, Oberon-2 Java, Smalltalk Functional Haskell, OCaml ML, Scheme, LISP Logic Prolog Mercury

FP 2005 1.7 9 What Kinds of Programming Languages are There? Imperative telling the machine what to do Declarative telling the machine what to achieve Programming Languages Imperative Declarative C, Pascal FORTRAN COBOL Modula-2 Object-oriented C++, Oberon-2 Java, Smalltalk Functional Haskell, OCaml ML, Scheme, LISP Logic Prolog Mercury

FP 2005 1.8 10 What Kinds of Programming Languages are There? Imperative telling the machine what to do Declarative telling the machine what to achieve Programming Languages Imperative Declarative C, Pascal FORTRAN COBOL Modula-2 Object-oriented C++, Oberon-2 Java, Smalltalk Functional Haskell, OCaml ML, Scheme, LISP Logic Prolog Mercury

FP 2005 1.9 11 Programming Language Paradigms Imperative Programming Languages Statement oriented languages Every statement changes the machine state Object-oriented languages Organising the state into objects with individual state and behaviour Message passing paradigm (instead of subprogram call) Rule-Based (Logical) Programming Languages Specify rule that specifies problem solution (Prolog, BNF Parsing) Other examples: Decision procedures, Grammar rules (BNF) Programming consists of specifying the attributes of the answer Functional (Applicative) Programming Languages Goal is to understand the function that produces the answer Function composition is major operation Programming consists of building the function that computes the answer

FP 2005 1.10 12 Historical Development of Programming Languages

FP 2005 1.11 13 Historical Development of Programming Languages Emphasis has changed:

FP 2005 1.12 14 Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer

FP 2005 1.13 15 Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer to making it easier for the programmer.

FP 2005 1.14 16 Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer to making it easier for the programmer. Easier for the programmer means:

FP 2005 1.15 17 Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer to making it easier for the programmer. Easier for the programmer means: Use languages that facilitate writing error-free programs

FP 2005 1.16 18 Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer to making it easier for the programmer. Easier for the programmer means: Use languages that facilitate writing error-free programs Use languages that facilitate writing programs that are easy to maintain

FP 2005 1.17 19 Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer to making it easier for the programmer. Easier for the programmer means: Use languages that facilitate writing error-free programs Use languages that facilitate writing programs that are easy to maintain Goal of language development:

FP 2005 1.18 20 Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer to making it easier for the programmer. Easier for the programmer means: Use languages that facilitate writing error-free programs Use languages that facilitate writing programs that are easy to maintain Goal of language development: Developers concentrate on design (or even just specification) Programming is trivial or handled by computer (executable specification languages, rapid prototyping)

FP 2005 1.19 21 Important Functional Programming Languages Functional Programming Languages pure, statically typed impure Haskell Clean statically typed dynamically typed Standard ML OCaml LISP, Scheme APL, J Erlang

FP 2005 1.20 22 Important Functional Programming Languages Functional Programming Languages pure, statically typed impure Haskell Clean statically typed dynamically typed Standard ML OCaml LISP, Scheme APL, J Erlang

FP 2005 1.21 23 Important Functional Programming Languages Functional Programming Languages pure, statically typed impure Haskell Clean statically typed dynamically typed Standard ML OCaml LISP, Scheme APL, J Erlang

FP 2005 1.22 24 Important Functional Programming Languages Functional Programming Languages pure, statically typed impure Haskell Clean statically typed dynamically typed Standard ML OCaml LISP, Scheme APL, J Erlang

FP 2005 1.23 25 Important Functional Programming Languages Functional Programming Languages pure, statically typed impure Haskell Clean statically typed dynamically typed Standard ML OCaml LISP, Scheme APL, J Erlang

FP 2005 1.24 26 Important Functional Programming Languages Functional Programming Languages pure, statically typed impure Haskell Clean statically typed dynamically typed Standard ML OCaml LISP, Scheme APL, J Erlang

FP 2005 1.25 27 Important Functional Programming Languages Functional Programming Languages pure, statically typed impure Haskell Clean statically typed dynamically typed Standard ML OCaml LISP, Scheme APL, J Erlang

FP 2005 2.1 29 Haskell

FP 2005 2.2 30 Haskell functional

FP 2005 2.3 31 Haskell functional programs are function definitions

FP 2005 2.4 32 Haskell functional programs are function definitions; functions are first-class citizens

FP 2005 2.5 33 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent)

FP 2005 2.6 34 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects

FP 2005 2.7 35 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy)

FP 2005 2.8 36 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy) arguments are evaluated only when needed

FP 2005 2.9 37 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy) arguments are evaluated only when needed statically strongly typed

FP 2005 2.10 38 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy) arguments are evaluated only when needed statically strongly typed all type errors caught at compile-time

FP 2005 2.11 39 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy) arguments are evaluated only when needed statically strongly typed all type errors caught at compile-time type classes safe overloading

FP 2005 2.12 40 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy) arguments are evaluated only when needed statically strongly typed all type errors caught at compile-time type classes safe overloading Standardised language version: Haskell 98

FP 2005 2.13 41 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy) arguments are evaluated only when needed statically strongly typed all type errors caught at compile-time type classes safe overloading Standardised language version: Haskell 98 Several compilers and interpreters available

FP 2005 2.14 42 Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy) arguments are evaluated only when needed statically strongly typed all type errors caught at compile-time type classes safe overloading Standardised language version: Haskell 98 Several compilers and interpreters available Comprehensive web site: http://haskell.org/

FP 2005 2.15 43 Important Points

FP 2005 2.16 44 Important Points Execution of Haskell programs

FP 2005 2.17 45 Important Points Execution of Haskell programs is expression evaluation

FP 2005 2.18 46 Important Points Execution of Haskell programs is expression evaluation (for the time being)

FP 2005 2.19 47 Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell

FP 2005 2.20 48 Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell is more like defining functions in mathematics

FP 2005 2.21 49 Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell is more like defining functions in mathematics than like defining procedures in C or classes and methods in Java

FP 2005 2.22 50 Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell is more like defining functions in mathematics than like defining procedures in C or classes and methods in Java One Haskell function may be defined by several equations

FP 2005 2.23 51 Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell is more like defining functions in mathematics than like defining procedures in C or classes and methods in Java One Haskell function may be defined by several equations the first that matches is used

FP 2005 2.24 52 Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell is more like defining functions in mathematics than like defining procedures in C or classes and methods in Java One Haskell function may be defined by several equations the first that matches is used Lists are an easy-to-use datastructure with lots of language and library support

FP 2005 2.25 53 Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell is more like defining functions in mathematics than like defining procedures in C or classes and methods in Java One Haskell function may be defined by several equations the first that matches is used Lists are an easy-to-use datastructure with lots of language and library support therefore, lists are heavily used in beginners material.

FP 2005 2.26 54 Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell is more like defining functions in mathematics than like defining procedures in C or classes and methods in Java One Haskell function may be defined by several equations the first that matches is used Lists are an easy-to-use datastructure with lots of language and library support therefore, lists are heavily used in beginners material. In many cases, advanced Haskell programmers will use other datastructures, for example Sets, or FiniteMaps instead of association lists.

FP 2005 2.27 55 Simple Expression Evaluation The Haskell interpreters hugs, ghci, and hi accept any expression at their prompt and print (after the first ENTER) the value resulting from evaluation of that expression. Prelude> 4*(5+6)-2 42 Expression evaluation proceeds by applying rules to subexpressions: 4*(5+6)-2 = (addition) 4*11-2 (multiplication)

FP 2005 2.28 56 Simple Expression Evaluation The Haskell interpreters hugs, ghci, and hi accept any expression at their prompt and print (after the first ENTER) the value resulting from evaluation of that expression. Prelude> 4*(5+6)-2 42 Expression evaluation proceeds by applying rules to subexpressions: 4*(5+6)-2 [subtraction & mult. impossible] = (addition) 4*11-2 (multiplication)

FP 2005 2.29 57 Simple Expression Evaluation The Haskell interpreters hugs, ghci, and hi accept any expression at their prompt and print (after the first ENTER) the value resulting from evaluation of that expression. Prelude> 4*(5+6)-2 42 Expression evaluation proceeds by applying rules to subexpressions: 4*(5+6)-2 [subtraction & mult. impossible] = (addition) 4*11-2 (multiplication)

FP 2005 2.30 58 Simple Expression Evaluation The Haskell interpreters hugs, ghci, and hi accept any expression at their prompt and print (after the first ENTER) the value resulting from evaluation of that expression. Prelude> 4*(5+6)-2 42 Expression evaluation proceeds by applying rules to subexpressions: 4*(5+6)-2 [subtraction & mult. impossible] = (addition) 4*11-2 [subtraction impossible] (multiplication)

FP 2005 2.31 59 Simple Expression Evaluation The Haskell interpreters hugs, ghci, and hi accept any expression at their prompt and print (after the first ENTER) the value resulting from evaluation of that expression. Prelude> 4*(5+6)-2 42 Expression evaluation proceeds by applying rules to subexpressions: 4*(5+6)-2 [subtraction & mult. impossible] = (addition) 4*11-2 [subtraction impossible] = (multiplication) 44-2

FP 2005 2.32 60 Simple Expression Evaluation The Haskell interpreters hugs, ghci, and hi accept any expression at their prompt and print (after the first ENTER) the value resulting from evaluation of that expression. Prelude> 4*(5+6)-2 42 Expression evaluation proceeds by applying rules to subexpressions: 4*(5+6)-2 [subtraction & mult. impossible] = (addition) 4*11-2 [subtraction impossible] = (multiplication) 44-2 = (subtraction) 42

FP 2005 2.33 61 Simple Expression Evaluation Explanation

FP 2005 2.34 62 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed.

FP 2005 2.35 63 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments,

FP 2005 2.36 64 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed

FP 2005 2.37 65 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument

FP 2005 2.38 66 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument

FP 2005 2.39 67 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f.

FP 2005 2.40 68 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff f undefined = undefined.

FP 2005 2.41 69 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff Constant functions are non-strict: f undefined = undefined.

FP 2005 2.42 70 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff f undefined = undefined. Constant functions are non-strict: ( const 5) undefined = 5

FP 2005 2.43 71 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff f undefined = undefined. Constant functions are non-strict: ( const 5) undefined = 5 Checking a list for emptyness is strict:

FP 2005 2.44 72 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff f undefined = undefined. Constant functions are non-strict: ( const 5) undefined = 5 Checking a list for emptyness is strict: null undefined = undefined

FP 2005 2.45 73 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff f undefined = undefined. Constant functions are non-strict: ( const 5) undefined = 5 Checking a list for emptyness is strict: null undefined = undefined List construction is non-strict:

FP 2005 2.46 74 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff f undefined = undefined. Constant functions are non-strict: ( const 5) undefined = 5 Checking a list for emptyness is strict: null undefined = undefined List construction is non-strict: null ( undefined : undefined ) = False

FP 2005 2.47 75 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff f undefined = undefined. Constant functions are non-strict: ( const 5) undefined = 5 Checking a list for emptyness is strict: null undefined = undefined List construction is non-strict: null ( undefined : undefined ) = False Standard arithmetic operators are strict in both arguments:

FP 2005 2.48 76 Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed. If for obtaining a result from an application of a function f to a number of arguments, the value of the argument at position i is always needed. then f is called strict in its i-th argument Therefore: If f is strict in its i-th argument, then the i-th argument has to be evaluated whenever a result is needed from f. Simpler: A one-argument function f is strict iff f undefined = undefined. Constant functions are non-strict: ( const 5) undefined = 5 Checking a list for emptyness is strict: null undefined = undefined List construction is non-strict: null ( undefined : undefined ) = False Standard arithmetic operators are strict in both arguments: 0 undefined = undefined

FP 2005 2.49 77 Unfolding Definitions Assume the following definitions to be in scope: answer = 42 magic = 7 Expression evaluation will unfold (or expand) definitions: Prelude> (answer - 1) * (magic * answer - 23) 11111

FP 2005 2.50 78 Unfolding Definitions Assume the following definitions to be in scope: answer = 42 magic = 7 Expression evaluation will unfold (or expand) definitions: Prelude> (answer - 1) * (magic * answer - 23) 11111 (answer - 1) * (magic * answer - 23)

FP 2005 2.51 79 Unfolding Definitions Assume the following definitions to be in scope: answer = 42 magic = 7 Expression evaluation will unfold (or expand) definitions: Prelude> (answer - 1) * (magic * answer - 23) 11111 (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer)

FP 2005 2.52 80 Unfolding Definitions Assume the following definitions to be in scope: answer = 42 magic = 7 Expression evaluation will unfold (or expand) definitions: Prelude> (answer - 1) * (magic * answer - 23) 11111 (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer) = 41 * (magic * 42-23) (subtraction)

FP 2005 2.53 81 Unfolding Definitions Assume the following definitions to be in scope: answer = 42 magic = 7 Expression evaluation will unfold (or expand) definitions: Prelude> (answer - 1) * (magic * answer - 23) 11111 (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer) = 41 * (magic * 42-23) (subtraction) = 41 * (7 * 42-23) (magic)

FP 2005 2.54 82 Unfolding Definitions Assume the following definitions to be in scope: answer = 42 magic = 7 Expression evaluation will unfold (or expand) definitions: Prelude> (answer - 1) * (magic * answer - 23) 11111 (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer) = 41 * (magic * 42-23) (subtraction) = 41 * (7 * 42-23) (magic) = 41 * (294-23) (multiplication)

FP 2005 2.55 83 Unfolding Definitions Assume the following definitions to be in scope: answer = 42 magic = 7 Expression evaluation will unfold (or expand) definitions: Prelude> (answer - 1) * (magic * answer - 23) 11111 (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer) = 41 * (magic * 42-23) (subtraction) = 41 * (7 * 42-23) (magic) = 41 * (294-23) (multiplication) = 41 * 271 (subtraction)

FP 2005 2.56 84 Unfolding Definitions Assume the following definitions to be in scope: answer = 42 magic = 7 Expression evaluation will unfold (or expand) definitions: Prelude> (answer - 1) * (magic * answer - 23) 11111 (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer) = 41 * (magic * 42-23) (subtraction) = 41 * (7 * 42-23) (magic) = 41 * (294-23) (multiplication) = 41 * 271 (subtraction) = 11111 (multiplication)

FP 2005 2.57 85 Easy! How did I find those numbers? Prelude> [ n n <- [1.. 400], 11111 mod n == 0 ] [1,41,271] This is a list comprehension: return all n where n is taken from then list [1.. 400] and a result is returned only if n divides 11111.

FP 2005 2.58 86 Conditional Expressions Prelude> if 11111 mod 41 == 0 then 11111 div 41 else 5 271 The pattern is: if condition then expression1 else expression2 If the condition evaluates to True, the conditional expression evaluates to the value of expression1. If the condition evaluates to False, the conditional expression evaluates to the value of expression2.

FP 2005 2.59 87 Conditional Expressions Prelude> if 11111 mod 41 == 0 then 11111 div 41 else 5 271 The pattern is: if condition then expression1 else expression2 If the condition evaluates to True, the conditional expression evaluates to the value of expression1. If the condition evaluates to False, the conditional expression evaluates to the value of expression2. Therefore: if _ then _ else is strict in the condition.

FP 2005 2.60 88 Conditional Expressions Prelude> if 11111 mod 41 == 0 then 11111 div 41 else 5 271 The pattern is: if condition then expression1 else expression2 If the condition evaluates to True, the conditional expression evaluates to the value of expression1. If the condition evaluates to False, the conditional expression evaluates to the value of expression2. Therefore: if _ then _ else is strict in the condition. In C: ( condition? expression1 : expression2 )

FP 2005 2.61 89 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1)

FP 2005 2.62 90 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3

FP 2005 2.63 91 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1)

FP 2005 2.64 92 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1)

FP 2005 2.65 93 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1)

FP 2005 2.66 94 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1)

FP 2005 2.67 95 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1)

FP 2005 2.68 96 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1)

FP 2005 2.69 97 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1)

FP 2005 2.70 98 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1)

FP 2005 2.71 99 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1)

FP 2005 2.72 100 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1)

FP 2005 2.73 101 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1) = 3 * 2 * 1 * fact (1-1)

FP 2005 2.74 102 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1) = 3 * 2 * 1 * fact (1-1) = 3 * 2 * 1 * if (1-1) == 0 then 1 else (1-1) * fact ((1-1)-1)

FP 2005 2.75 103 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1) = 3 * 2 * 1 * fact (1-1) = 3 * 2 * 1 * if (1-1) == 0 then 1 else (1-1) * fact ((1-1)-1) = 3 * 2 * 1 * if 0 == 0 then 1 else 0 * fact (0-1)

FP 2005 2.76 104 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1) = 3 * 2 * 1 * fact (1-1) = 3 * 2 * 1 * if (1-1) == 0 then 1 else (1-1) * fact ((1-1)-1) = 3 * 2 * 1 * if 0 == 0 then 1 else 0 * fact (0-1) = 3 * 2 * 1 * if True then 1 else 0 * fact (0-1)

FP 2005 2.77 105 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1) = 3 * 2 * 1 * fact (1-1) = 3 * 2 * 1 * if (1-1) == 0 then 1 else (1-1) * fact ((1-1)-1) = 3 * 2 * 1 * if 0 == 0 then 1 else 0 * fact (0-1) = 3 * 2 * 1 * if True then 1 else 0 * fact (0-1) = 3 * 2 * 1 * 1

FP 2005 2.78 106 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1) = 3 * 2 * 1 * fact (1-1) = 3 * 2 * 1 * if (1-1) == 0 then 1 else (1-1) * fact ((1-1)-1) = 3 * 2 * 1 * if 0 == 0 then 1 else 0 * fact (0-1) = 3 * 2 * 1 * if True then 1 else 0 * fact (0-1) = 3 * 2 * 1 * 1 = 3 * 2 * 1

FP 2005 2.79 107 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1) = 3 * 2 * 1 * fact (1-1) = 3 * 2 * 1 * if (1-1) == 0 then 1 else (1-1) * fact ((1-1)-1) = 3 * 2 * 1 * if 0 == 0 then 1 else 0 * fact (0-1) = 3 * 2 * 1 * if True then 1 else 0 * fact (0-1) = 3 * 2 * 1 * 1 = 3 * 2 * 1 = 3 * 2

FP 2005 2.80 108 Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3 = if 3 == 0 then 1 else 3 * fact (3-1) = if False then 1 else 3 * fact (3-1) = 3 * fact (3-1) = 3 * if (3-1) == 0 then 1 else (3-1) * fact ((3-1)-1) = 3 * if 2 == 0 then 1 else 2 * fact (2-1) = 3 * if False then 1 else 2 * fact (2-1) = 3 * 2 * fact (2-1) = 3 * 2 * if (2-1) == 0 then 1 else (2-1) * fact ((2-1)-1) = 3 * 2 * if 1 == 0 then 1 else 1 * fact (1-1) = 3 * 2 * if False then 1 else 1 * fact (1-1) = 3 * 2 * 1 * fact (1-1) = 3 * 2 * 1 * if (1-1) == 0 then 1 else (1-1) * fact ((1-1)-1) = 3 * 2 * 1 * if 0 == 0 then 1 else 0 * fact (0-1) = 3 * 2 * 1 * if True then 1 else 0 * fact (0-1) = 3 * 2 * 1 * 1 = 3 * 2 * 1 = 3 * 2 = 6

FP 2005 2.81 109 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions

FP 2005 2.82 110 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * (2 * (1 * fact (1-1)))

FP 2005 2.83 111 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * (2 * (1 * fact (1-1)))

FP 2005 2.84 112 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * (1 * fact (1-1)))

FP 2005 2.85 113 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * fact (2-1)) (fact n) = 3 * (2 * (1 * fact (1-1)))

FP 2005 2.86 114 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * fact (2-1)) (fact n) = 3 * (2 * fact 1) (determining which fact rule matches) = 3 * (2 * (1 * fact (1-1)))

FP 2005 2.87 115 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * fact (2-1)) (fact n) = 3 * (2 * fact 1) (determining which fact rule matches) = 3 * (2 * (1 * fact (1-1))) (fact n)

FP 2005 2.88 116 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * fact (2-1)) (fact n) = 3 * (2 * fact 1) (determining which fact rule matches) = 3 * (2 * (1 * fact (1-1))) (fact n) = 3 * (2 * (1 * fact 0)) (determining which fact rule matches)

FP 2005 2.89 117 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * fact (2-1)) (fact n) = 3 * (2 * fact 1) (determining which fact rule matches) = 3 * (2 * (1 * fact (1-1))) (fact n) = 3 * (2 * (1 * fact 0)) (determining which fact rule matches) = 3 * (2 * (1 * 1)) (fact 0)

FP 2005 2.90 118 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * fact (2-1)) (fact n) = 3 * (2 * fact 1) (determining which fact rule matches) = 3 * (2 * (1 * fact (1-1))) (fact n) = 3 * (2 * (1 * fact 0)) (determining which fact rule matches) = 3 * (2 * (1 * 1)) (fact 0) = 3 * (2 * 1) (multiplication)

FP 2005 2.91 119 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * fact (2-1)) (fact n) = 3 * (2 * fact 1) (determining which fact rule matches) = 3 * (2 * (1 * fact (1-1))) (fact n) = 3 * (2 * (1 * fact 0)) (determining which fact rule matches) = 3 * (2 * (1 * 1)) (fact 0) = 3 * (2 * 1) (multiplication) = 3 * 2 (multiplication)

FP 2005 2.92 120 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * fact (3-1) (fact n) = 3 * fact 2 (determining which fact rule matches) = 3 * (2 * fact (2-1)) (fact n) = 3 * (2 * fact 1) (determining which fact rule matches) = 3 * (2 * (1 * fact (1-1))) (fact n) = 3 * (2 * (1 * fact 0)) (determining which fact rule matches) = 3 * (2 * (1 * 1)) (fact 0) = 3 * (2 * 1) (multiplication) = 3 * 2 (multiplication) = 6 (multiplication)

FP 2005 2.93 121 Lists List display: between square brackets explicitly listing all elements, separated by commas: [1,4,9,16,25]

FP 2005 2.94 122 Lists List display: between square brackets explicitly listing all elements, separated by commas: [1,4,9,16,25] Enumeration lists: denoted by ellipsis.. inside square brackets; defined by beginning (and end, if applicable): [1.. 10] = [1,2,3,4,5,6,7,8,9,10] [1,3.. 10] = [1,3,5,7,9] [1,3.. 11] = [1,3,5,7,9,11] [11,9.. 1] = [11,9,7,5,3,1] [11.. 1] = [] [1.. ] = [1,2,3,4,5,6,7,8,9,10, ] -- infinite list [1,3.. ] = [1,3,5,7,9,11, ] -- infinite list

FP 2005 2.95 123 List Construction

FP 2005 2.96 124 List Construction Display and enumeration lists are syntactic sugar

FP 2005 2.97 125 List Construction Display and enumeration lists are syntactic sugar: A list is

FP 2005 2.98 126 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty

FP 2005 2.99 127 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs

FP 2005 2.100 128 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes )

FP 2005 2.101 129 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes.

FP 2005 2.102 130 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor:

FP 2005 2.103 131 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : []

FP 2005 2.104 132 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3]

FP 2005 2.105 133 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3]

FP 2005 2.106 134 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3]

FP 2005 2.107 135 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3] 1 : [2, 3]

FP 2005 2.108 136 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3] 1 : [2, 3] = [1, 2, 3]

FP 2005 2.109 137 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3] 1 : [2, 3] = [1, 2, 3] As an infix operator, : associates to the right

FP 2005 2.110 138 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3] 1 : [2, 3] = [1, 2, 3] As an infix operator, : associates to the right: x : y : ys = x : ( y : ys )

FP 2005 2.111 139 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3] 1 : [2, 3] = [1, 2, 3] As an infix operator, : associates to the right: Example: 1 : 2 : [3,4] x : y : ys = x : ( y : ys )

FP 2005 2.112 140 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3] 1 : [2, 3] = [1, 2, 3] As an infix operator, : associates to the right: Example: 1 : 2 : [3,4] = 1 : (2 : [3, 4]) x : y : ys = x : ( y : ys )

FP 2005 2.113 141 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3] 1 : [2, 3] = [1, 2, 3] As an infix operator, : associates to the right: Example: x : y : ys = x : ( y : ys ) 1 : 2 : [3,4] = 1 : (2 : [3, 4]) = 1 : [2, 3, 4]

FP 2005 2.114 142 List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty, and constructed from a head x and a tail xs (read: xes ) x : xs read: x cons xes. : is used as infix list constructor: 3 : [] = [3] 2 : [3] = [2, 3] 1 : [2, 3] = [1, 2, 3] As an infix operator, : associates to the right: Example: x : y : ys = x : ( y : ys ) 1 : 2 : [3,4] = 1 : (2 : [3, 4]) = 1 : [2, 3, 4] = [1, 2, 3, 4]

FP 2005 2.115 143 Cons is Not Associative

FP 2005 2.116 144 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances.

FP 2005 2.117 145 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative:

FP 2005 2.118 146 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4])

FP 2005 2.119 147 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4]

FP 2005 2.120 148 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4]

FP 2005 2.121 149 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4]

FP 2005 2.122 150 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4] is nonsense

FP 2005 2.123 151 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4] is nonsense, since 2 is not a list!

FP 2005 2.124 152 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4] is nonsense, since 2 is not a list! A list of lists of integers: [2] : [[3,4,5], [6,7]]

FP 2005 2.125 153 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4] is nonsense, since 2 is not a list! A list of lists of integers: [2] : [[3,4,5], [6,7]] = [[2],[3,4,5],[6,7]]

FP 2005 2.126 154 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4] is nonsense, since 2 is not a list! A list of lists of integers: [2] : [[3,4,5], [6,7]] = [[2],[3,4,5],[6,7]] Another list of lists of integers: (1 : [2]) : [[3,4,5], [6,7]]

FP 2005 2.127 155 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4] is nonsense, since 2 is not a list! A list of lists of integers: [2] : [[3,4,5], [6,7]] = [[2],[3,4,5],[6,7]] Another list of lists of integers: (1 : [2]) : [[3,4,5], [6,7]] = [[1,2],[3,4,5],[6,7]]

FP 2005 2.128 156 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4] is nonsense, since 2 is not a list! A list of lists of integers: [2] : [[3,4,5], [6,7]] = [[2],[3,4,5],[6,7]] Another list of lists of integers: (1 : [2]) : [[3,4,5], [6,7]] = [[1,2],[3,4,5],[6,7]] 1 : ([2] : [[3,4,5], [6,7]])

FP 2005 2.129 157 Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative: A list of integers: 1 : (2 : [3,4]) = 1 : 2 : [3,4] = [1, 2, 3, 4] (1 : 2) : [3,4] is nonsense, since 2 is not a list! A list of lists of integers: [2] : [[3,4,5], [6,7]] = [[2],[3,4,5],[6,7]] Another list of lists of integers: (1 : [2]) : [[3,4,5], [6,7]] = [[1,2],[3,4,5],[6,7]] 1 : ([2] : [[3,4,5], [6,7]]) is nonsense again!