Functional Programming
|
|
|
- Karin Shelton
- 9 years ago
- Views:
Transcription
1 FP Functional Programming WOLFRAM KAHL [email protected] Department of Computing and Software McMaster University
2 FP 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
3 FP 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
4 FP 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
5 FP 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
6 FP 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
7 FP 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
8 FP 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
9 FP 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
10 FP Historical Development of Programming Languages
11 FP Historical Development of Programming Languages Emphasis has changed:
12 FP Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer
13 FP Historical Development of Programming Languages Emphasis has changed: from making life easier for the computer to making it easier for the programmer.
14 FP 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:
15 FP 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
16 FP 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
17 FP 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:
18 FP 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)
19 FP 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
20 FP 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
21 FP 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
22 FP 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
23 FP 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
24 FP 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
25 FP 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
26 FP Haskell
27 FP Haskell functional
28 FP Haskell functional programs are function definitions
29 FP Haskell functional programs are function definitions; functions are first-class citizens
30 FP Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent)
31 FP Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects
32 FP Haskell functional programs are function definitions; functions are first-class citizens pure (referentially transparent) no side-effects non-strict (lazy)
33 FP 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
34 FP 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
35 FP 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
36 FP 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
37 FP 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
38 FP 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
39 FP 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:
40 FP Important Points
41 FP Important Points Execution of Haskell programs
42 FP Important Points Execution of Haskell programs is expression evaluation
43 FP Important Points Execution of Haskell programs is expression evaluation (for the time being)
44 FP Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell
45 FP Important Points Execution of Haskell programs is expression evaluation (for the time being) Defining functions in Haskell is more like defining functions in mathematics
46 FP 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
47 FP 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
48 FP 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
49 FP 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
50 FP 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.
51 FP 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.
52 FP 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)
53 FP 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)
54 FP 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)
55 FP 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)
56 FP 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
57 FP 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
58 FP Simple Expression Evaluation Explanation
59 FP Simple Expression Evaluation Explanation Arguments to a fuction or operation are evaluated only when needed.
60 FP 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,
61 FP 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
62 FP 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
63 FP 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
64 FP 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.
65 FP 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.
66 FP 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.
67 FP 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
68 FP 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:
69 FP 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
70 FP 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:
71 FP 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
72 FP 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:
73 FP 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
74 FP 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
75 FP 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) (answer - 1) * (magic * answer - 23)
76 FP 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) (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer)
77 FP 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) (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer) = 41 * (magic * 42-23) (subtraction)
78 FP 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) (answer - 1) * (magic * answer - 23) = (42-1) * (magic * 42-23) (answer) = 41 * (magic * 42-23) (subtraction) = 41 * (7 * 42-23) (magic)
79 FP 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) (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)
80 FP 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) (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)
81 FP 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) (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) = (multiplication)
82 FP Easy! How did I find those numbers? Prelude> [ n n <- [ ], mod n == 0 ] [1,41,271] This is a list comprehension: return all n where n is taken from then list [ ] and a result is returned only if n divides
83 FP Conditional Expressions Prelude> if mod 41 == 0 then div 41 else 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.
84 FP Conditional Expressions Prelude> if mod 41 == 0 then div 41 else 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.
85 FP Conditional Expressions Prelude> if mod 41 == 0 then div 41 else 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 )
86 FP Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1)
87 FP Expanding Function Definitions fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n-1) fact 3
88 FP 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)
89 FP 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)
90 FP 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)
91 FP 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)
92 FP 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)
93 FP 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)
94 FP 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)
95 FP 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)
96 FP 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)
97 FP 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)
98 FP 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)
99 FP 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)
100 FP 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)
101 FP 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)
102 FP 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
103 FP 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
104 FP 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
105 FP 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
106 FP fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions
107 FP fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n-1) Matching Function Definitions fact 3 = 3 * (2 * (1 * fact (1-1)))
108 FP 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)))
109 FP 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)))
110 FP 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)))
111 FP 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)))
112 FP 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)
113 FP 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)
114 FP 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)
115 FP 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)
116 FP 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)
117 FP 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)
118 FP Lists List display: between square brackets explicitly listing all elements, separated by commas: [1,4,9,16,25]
119 FP 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
120 FP List Construction
121 FP List Construction Display and enumeration lists are syntactic sugar
122 FP List Construction Display and enumeration lists are syntactic sugar: A list is
123 FP List Construction Display and enumeration lists are syntactic sugar: A list is either the empty list: [], or non-empty
124 FP 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
125 FP 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 )
126 FP 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.
127 FP 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:
128 FP 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 : []
129 FP 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]
130 FP 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]
131 FP 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]
132 FP 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]
133 FP 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]
134 FP 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
135 FP 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 )
136 FP 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 )
137 FP 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 )
138 FP 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]
139 FP 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]
140 FP Cons is Not Associative
141 FP Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances.
142 FP Cons is Not Associative The convention that : associates to the right allows to save parentheses in certain cirtcumstances. However, : is not associative:
143 FP 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])
144 FP 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]
145 FP 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]
146 FP 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]
147 FP 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
148 FP 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!
149 FP 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]]
150 FP 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]]
151 FP 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]]
152 FP 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]]
153 FP 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]])
154 FP 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!
155 FP 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! Reason: 1 and [2] cannot be members of the same list (type error).
156 FP List Comprehensions General shape: [ term generator {, generator_or_constraint } ]
157 FP General shape: Examples: [ n n n [1.. 5] ] List Comprehensions [ term generator {, generator_or_constraint } ]
158 FP List Comprehensions General shape: [ term generator {, generator_or_constraint } ] Examples: [ n n n [1.. 5] ] = [1,4,9,16,25]
159 FP General shape: Examples: List Comprehensions [ term generator {, generator_or_constraint [ n n n [1.. 5] ] = [1,4,9,16,25] [ n n n [1.. 10], even n ] } ]
160 FP General shape: Examples: List Comprehensions [ term generator {, generator_or_constraint [ n n n [1.. 5] ] = [1,4,9,16,25] [ n n n [1.. 10], even n ] = [4,16,36,64,100] } ]
161 FP General shape: Examples: List Comprehensions [ term generator {, generator_or_constraint [ n n n [1.. 5] ] = [1,4,9,16,25] [ n n n [1.. 10], even n ] = [4,16,36,64,100] [ m n m [1,3,5], n [2,4,6] ] } ]
162 FP General shape: Examples: List Comprehensions [ term generator {, generator_or_constraint [ n n n [1.. 5] ] = [1,4,9,16,25] [ n n n [1.. 10], even n ] = [4,16,36,64,100] } ] [ m n m [1,3,5], n [2,4,6] ] = [2,4,6,6,12,18,10,20,30]
163 FP General shape: Examples: List Comprehensions [ term generator {, generator_or_constraint [ n n n [1.. 5] ] = [1,4,9,16,25] [ n n n [1.. 10], even n ] = [4,16,36,64,100] } ] [ m n m [1,3,5], n [2,4,6] ] = [2,4,6,6,12,18,10,20,30] Note: The left generator generates slower.
164 FP General shape: Examples: List Comprehensions [ term generator {, generator_or_constraint [ n n n [1.. 5] ] = [1,4,9,16,25] [ n n n [1.. 10], even n ] = [4,16,36,64,100] } ] [ m n m [1,3,5], n [2,4,6] ] = [2,4,6,6,12,18,10,20,30] Note: The left generator generates slower. Haskell code fragments will frequently be presented like above in a form that is more readable than plain typewriter text in that case, the comes from arrow <- in generators turns into
165 FP The Type Language Haskell has a full-fledged type language, with Simple predefined datatypes: Bool, Char, Integer, Predefined type constructors: lists, tuples, functions, Type synonyms User-defined datatypes and type constructors Type variables to express parametric polymorphism
166 FP Simple Predefined Datatypes Bool truth values False, True Char Unicode characters (in GHC: ISO-10646) Integer integers arbitrary precision Int machine integers 32 bits Float real floating point single precision Double real floating point double precision Complex Float complex floating point single precision Complex Double complex floating point double precision
167 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t.
168 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100
169 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] ::???
170 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer]
171 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] ::???
172 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int]
173 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] ::???
174 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]]
175 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]] [ h, e, l, l, o ] ::???
176 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]] [ h, e, l, l, o ] :: [Char]
177 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]] [ h, e, l, l, o ] :: [Char] "hello" ::???
178 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]] [ h, e, l, l, o ] :: [Char] "hello" :: [Char]
179 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]] [ h, e, l, l, o ] :: [Char] "hello" :: [Char] [ "hello", "world" ] ::???
180 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]] [ h, e, l, l, o ] :: [Char] "hello" :: [Char] [ "hello", "world" ] :: [[Char]]
181 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]] [ h, e, l, l, o ] :: [Char] "hello" :: [Char] [ "hello", "world" ] :: [[Char]] [["first", "line"], ["second", "line"]] ::???
182 FP List Types If t is a type, then the list type [t] is the type of lists with elements of type t. answer :: Integer answer = 42 limit :: Int limit = 100 Then: [ 1, 2, 3, answer] :: [Integer] [ 1.. limit ] :: [Int] [ [ 1.. limit ], [ 2.. limit ] ] :: [[Int]] [ h, e, l, l, o ] :: [Char] "hello" :: [Char] [ "hello", "world" ] :: [[Char]] [["first", "line"], ["second", "line"]] :: [[[Char]]]
183 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u).
184 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) ::???
185 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int)
186 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) ::???
187 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer)
188 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) ::???
189 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer)
190 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer) ("???", (limit, answer)) ::???
191 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer) ("???", (limit, answer)) :: ([Char], (Int, Integer))
192 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer) ("???", (limit, answer)) :: ([Char], (Int, Integer)) ("???", X ) ::???
193 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer) ("???", (limit, answer)) :: ([Char], (Int, Integer)) ("???", X ) :: ([Char], Char)
194 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer) ("???", (limit, answer)) :: ([Char], (Int, Integer)) ("???", X ) :: ([Char], Char) (limit, ("???", X )) ::???
195 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer) ("???", (limit, answer)) :: ([Char], (Int, Integer)) ("???", X ) :: ([Char], Char) (limit, ("???", X )) :: (Int, ([Char], Char))
196 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer) ("???", (limit, answer)) :: ([Char], (Int, Integer)) ("???", X ) :: ([Char], Char) (limit, ("???", X )) :: (Int, ([Char], Char)) (True, [("X",limit),("Y",5)]) ::???
197 FP Product Types (Pairs) If t and u are types, then the product type (t,u) is the type of pairs with first component of type t and second component of type u (mathematically: t u). Examples: (answer, limit) :: (Integer, Int) (limit, answer) :: (Int, Integer) ("???", answer) :: ([Char], Integer) ("???", (limit, answer)) :: ([Char], (Int, Integer)) ("???", X ) :: ([Char], Char) (limit, ("???", X )) :: (Int, ([Char], Char)) (True, [("X",limit),("Y",5)]) :: (Bool, [([Char], Int)])
198 FP Tuple Types If n / 1 is a natural number and t 1,, t n are types, then the tuple type ( t 1,, t n ) is the type of n-tuples with the ith component of type t i. Examples: (answer, c, limit) :: (Integer, Char, Int) (answer, c, limit, "all") :: (Integer, Char, Int, [Char]) () :: () there is exactly one zero-tuple. The type () of zero-tuples is also called the unit type.
199 FP Simple Type Synonyms If t is a type not containing any type variables, and Name is an identifier with a capital first letter, then type Name = t defines Name as a type synonym for t, i.e., Name can now be used interchangeably with t. Examples: type String = [Char] predefined type Point = (Double, Double) (1.5, 2.7) type Triangle = (Point, Point, Point) type CharEntity = (Char, String) ( ü, "ü") type Dictionary = [(String,String)] [("day","jour")]
200 FP Type Variables and Polymorphic Types Identifiers with lower-case first letter can be used as type variables. Type variables can be used like other types in the construction of types, e.g.: [(a,b)] (Bool, (a, Int)) [ ( String, [(key, val)] ) ] A type containing at least one type variable is called polymorphic Polymorphic types can be instantiated by instantiating type variables with types, e.g.: [(a,b)] [(Char,b)] [(a,b)] [(Char,Int)] [(a,b)] [(a,[(string,int)])] [(a,b)] [(a,[(string,c)])]
201 FP Typing of List Construction The empty list can be used at any list type: [] :: [a] If an element x :: a and a list xs :: [a] are given, then (x : xs) :: [a] Examples: 2 :: Int [] :: [Int] [2] = 2 : [] :: [Int] [[3,4,5], [6,7]] :: [[Int]] [2] : [[3,4,5], [6,7]] :: [[Int]] 1 : ([2] : [[3,4,5], [6,7]]) cannot be typed!
202 FP Function Types and Function Application If t and u are types, then the function type t->u is the type of all functions accepting arguments of type t and producing results of type u (mathematically: t u). Then: If a function f :: a -> b and an argument x :: a are given, then we have (f x) :: b. If a function f :: a -> b is given and we know that (f x) :: b, then the argument x is used at type a. If an argument x :: a is given and we know that (f x) :: b, then the function f is used at type a -> b.
203 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False)
204 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char
205 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)]
206 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)] x :: String
207 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)] x :: String f p = limit + fst p
208 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)] x :: String f p = limit + fst p p :: (Int,a)
209 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)] x :: String f p = limit + fst p p :: (Int,a) f :: (Int,a) -> Int
210 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)] x :: String f p = limit + fst p p :: (Int,a) f :: (Int,a) -> Int g h = fst (h "") : [limit]
211 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)] x :: String f p = limit + fst p p :: (Int,a) f :: (Int,a) -> Int g h = fst (h "") : [limit] h :: String -> (Int,a)
212 FP Let s Play the Evaluation Game Again 1 h1 :: String -> (Int, String) h1 str = (length str, : str) g h = fst (h "") : [limit]
213 FP Let s Play the Evaluation Game Again 1 h1 :: String -> (Int, String) h1 str = (length str, : str) g h = fst (h "") : [limit] Then: g h1
214 FP Let s Play the Evaluation Game Again 1 h1 :: String -> (Int, String) h1 str = (length str, : str) g h = fst (h "") : [limit] Then: g h1 = fst (h1 "") : [limit]
215 FP Let s Play the Evaluation Game Again 1 h1 :: String -> (Int, String) h1 str = (length str, : str) g h = fst (h "") : [limit] Then: g h1 = fst (h1 "") : [limit] = fst (length "", : "") : [limit]
216 FP Let s Play the Evaluation Game Again 1 h1 :: String -> (Int, String) h1 str = (length str, : str) g h = fst (h "") : [limit] Then: g h1 = fst (h1 "") : [limit] = fst (length "", : "") : [limit] = length "" : [limit]
217 FP Let s Play the Evaluation Game Again 1 h1 :: String -> (Int, String) h1 str = (length str, : str) g h = fst (h "") : [limit] Then: g h1 = fst (h1 "") : [limit] = fst (length "", : "") : [limit] = length "" : [limit] = 0 : [limit]
218 FP Let s Play the Evaluation Game Again 1 h1 :: String -> (Int, String) h1 str = (length str, : str) g h = fst (h "") : [limit] Then: g h1 = fst (h1 "") : [limit] = fst (length "", : "") : [limit] = length "" : [limit] = 0 : [limit] = [0, 100]
219 FP Let s Play the Evaluation Game Again 2 h2 :: String -> (Int, Char) h2 str = (sum (map ord (notocccaps str)), head str) notocccaps :: String -> String notocccaps str = filter ( notelem str) [ A.. Z ] g h = fst (h "") : [limit]
220 FP Let s Play the Evaluation Game Again 2 h2 :: String -> (Int, Char) h2 str = (sum (map ord (notocccaps str)), head str) notocccaps :: String -> String notocccaps str = filter ( notelem str) [ A.. Z ] g h = fst (h "") : [limit] Then: g h2
221 FP Let s Play the Evaluation Game Again 2 h2 :: String -> (Int, Char) h2 str = (sum (map ord (notocccaps str)), head str) notocccaps :: String -> String notocccaps str = filter ( notelem str) [ A.. Z ] g h = fst (h "") : [limit] Then: g h2 = fst (h2 "") : [limit]
222 FP Let s Play the Evaluation Game Again 2 h2 :: String -> (Int, Char) h2 str = (sum (map ord (notocccaps str)), head str) notocccaps :: String -> String notocccaps str = filter ( notelem str) [ A.. Z ] g h = fst (h "") : [limit] Then: g h2 = fst (h2 "") : [limit] = fst (sum (map ord (notocccaps "")), head "") : [limit]
223 FP Let s Play the Evaluation Game Again 2 h2 :: String -> (Int, Char) h2 str = (sum (map ord (notocccaps str)), head str) notocccaps :: String -> String notocccaps str = filter ( notelem str) [ A.. Z ] g h = fst (h "") : [limit] Then: g h2 = fst (h2 "") : [limit] = fst (sum (map ord (notocccaps "")), head "") : [limit] = sum (map ord (notocccaps "")) : [limit]
224 FP Let s Play the Evaluation Game Again 2 h2 :: String -> (Int, Char) h2 str = (sum (map ord (notocccaps str)), head str) notocccaps :: String -> String notocccaps str = filter ( notelem str) [ A.. Z ] g h = fst (h "") : [limit] Then: g h2 = fst (h2 "") : [limit] = fst (sum (map ord (notocccaps "")), head "") : [limit] = sum (map ord (notocccaps "")) : [limit] =
225 FP Let s Play the Evaluation Game Again 2 h2 :: String -> (Int, Char) h2 str = (sum (map ord (notocccaps str)), head str) notocccaps :: String -> String notocccaps str = filter ( notelem str) [ A.. Z ] g h = fst (h "") : [limit] Then: g h2 = fst (h2 "") : [limit] = fst (sum (map ord (notocccaps "")), head "") : [limit] = sum (map ord (notocccaps "")) : [limit] = = 2015 : [limit]
226 FP Let s Play the Evaluation Game Again 2 h2 :: String -> (Int, Char) h2 str = (sum (map ord (notocccaps str)), head str) notocccaps :: String -> String notocccaps str = filter ( notelem str) [ A.. Z ] g h = fst (h "") : [limit] Then: g h2 = fst (h2 "") : [limit] = fst (sum (map ord (notocccaps "")), head "") : [limit] = sum (map ord (notocccaps "")) : [limit] = = 2015 : [limit] = [2015, 100]
227 FP g h = fst (h "") : [limit] Higher-Order Functions
228 FP Higher-Order Functions g h = fst (h "") : [limit] Functional Programming: Functions are first-class citizens
229 FP Higher-Order Functions g h = fst (h "") : [limit] Functional Programming: Functions are first-class citizens Functions can be arguments of other functions: g h2
230 FP Higher-Order Functions g h = fst (h "") : [limit] Functional Programming: Functions are first-class citizens Functions can be arguments of other functions: g h2 Functions can be components of data structures: (7,h1), [h1, h2]
231 FP Higher-Order Functions g h = fst (h "") : [limit] Functional Programming: Functions are first-class citizens Functions can be arguments of other functions: g h2 Functions can be components of data structures: (7,h1), [h1, h2] Functions can be results of function application: succ. succ
232 FP Higher-Order Functions g h = fst (h "") : [limit] Functional Programming: Functions are first-class citizens Functions can be arguments of other functions: g h2 Functions can be components of data structures: (7,h1), [h1, h2] Functions can be results of function application: succ. succ A first-order function accepts only non-functional values as arguments. A higher-order function expects functions as arguments.
233 FP Higher-Order Functions g h = fst (h "") : [limit] Functional Programming: Functions are first-class citizens Functions can be arguments of other functions: g h2 Functions can be components of data structures: (7,h1), [h1, h2] Functions can be results of function application: succ. succ A first-order function accepts only non-functional values as arguments. A higher-order function expects functions as arguments. g is a second-order function: it expects first-order functions like h1, h2 as arguments.
234 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)] x :: String f p = limit + fst p p :: (Int,a) f :: (Int,a) -> Int g h = fst (h "") : [limit] h :: String -> (Int,a)
235 FP fst :: (a,b) -> a fst (x,y) = x Type Inference Examples fst ( c, False) :: Char ["hello", fst (x, 17)] x :: String f p = limit + fst p p :: (Int,a) f :: (Int,a) -> Int g h = fst (h "") : [limit] h :: String -> (Int,a) g :: (String -> (Int,a)) -> [Int]
236 FP Curried Functions Function application associates to the left, i.e., f x y = (f x) y
237 FP Curried Functions Function application associates to the left, i.e., f x y = (f x) y Multi-argument functions in Haskell are typically defined as curried function, i.e., they accept their arguments one at a time : cylvol r h = (pi :: Double) * r * r * h
238 FP Curried Functions Function application associates to the left, i.e., f x y = (f x) y Multi-argument functions in Haskell are typically defined as curried function, i.e., they accept their arguments one at a time : cylvol r h = (pi :: Double) * r * r * h Since the right-hand side, r, and h obviously all have type Double, we have; (cylvol r) ::???
239 FP Curried Functions Function application associates to the left, i.e., f x y = (f x) y Multi-argument functions in Haskell are typically defined as curried function, i.e., they accept their arguments one at a time : cylvol r h = (pi :: Double) * r * r * h Since the right-hand side, r, and h obviously all have type Double, we have; (cylvol r) :: Double -> Double
240 FP Curried Functions Function application associates to the left, i.e., f x y = (f x) y Multi-argument functions in Haskell are typically defined as curried function, i.e., they accept their arguments one at a time : cylvol r h = (pi :: Double) * r * r * h Since the right-hand side, r, and h obviously all have type Double, we have; (cylvol r) :: Double -> Double cylvol ::???
241 FP Curried Functions Function application associates to the left, i.e., f x y = (f x) y Multi-argument functions in Haskell are typically defined as curried function, i.e., they accept their arguments one at a time : cylvol r h = (pi :: Double) * r * r * h Since the right-hand side, r, and h obviously all have type Double, we have; (cylvol r) :: Double -> Double cylvol :: Double -> (Double -> Double)
242 FP Curried Functions Function application associates to the left, i.e., f x y = (f x) y Multi-argument functions in Haskell are typically defined as curried function, i.e., they accept their arguments one at a time : cylvol r h = (pi :: Double) * r * r * h Since the right-hand side, r, and h obviously all have type Double, we have; (cylvol r) :: Double -> Double cylvol :: Double -> (Double -> Double) Function type construction associates to the right, i.e., a -> b -> c = a -> (b -> c)
243 FP Partial Application Let values with the following types be given: f :: a b c x :: a y :: b
244 FP Partial Application Let values with the following types be given: f :: a b c x :: a y :: b The type of f is the function type a ( b c )
245 FP Partial Application Let values with the following types be given: f :: a b c x :: a y :: b The type of f is the function type a ( b c ), with argument type a, result type b c.
246 FP Partial Application Let values with the following types be given: f :: a b c x :: a y :: b The type of f is the function type a ( b c ), with argument type a, result type b c. Therefore, we can apply f to x and obtain:
247 FP Partial Application Let values with the following types be given: f :: a b c x :: a y :: b The type of f is the function type a ( b c ), with argument type a, result type b c. Therefore, we can apply f to x and obtain: ( f x ) :: b c
248 FP Partial Application Let values with the following types be given: f :: a b c x :: a y :: b The type of f is the function type a ( b c ), with argument type a, result type b c. Therefore, we can apply f to x and obtain: ( f x ) :: b c The application of a two-argument function to a single argument is a one-argument function
249 FP Partial Application Let values with the following types be given: f :: a b c x :: a y :: b The type of f is the function type a ( b c ), with argument type a, result type b c. Therefore, we can apply f to x and obtain: ( f x ) :: b c The application of a two-argument function to a single argument is a one-argument function, which can then be applied to a second argument: ( f x ) y :: c = f x y
250 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example
251 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k n str = ( n ( length str + 1), unwords ( replicate n str ) )
252 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate n str ) )
253 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate n str ) ) g ( k 3)
254 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate n str ) ) g ( k 3) = fst ( k 3 "") : [ limit ]
255 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate n str ) ) g ( k 3) = fst ( k 3 "") : [ limit ] = fst (3 ( length "" + 1), unwords ( replicate 3 "") ) : [ limit ]
256 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate n str ) ) g ( k 3) = fst ( k 3 "") : [ limit ] = fst (3 ( length "" + 1), unwords ( replicate 3 "") ) : [ limit ] = (3 ( length "" + 1) ) : [ limit ]
257 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate n str ) ) g ( k 3) = fst ( k 3 "") : [ limit ] = fst (3 ( length "" + 1), unwords ( replicate 3 "") ) : [ limit ] = (3 ( length "" + 1) ) : [ limit ] = (3 (0 + 1) ) : [ limit ]
258 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate 3 str ) ) g ( k 3) = fst ( k 3 "") : [ limit ] = fst (3 ( length "" + 1), unwords ( replicate n "") ) : [ limit ] = (3 ( length "" + 1) ) : [ limit ] = (3 (0 + 1) ) : [ limit ] = (3 1) : [ limit ]
259 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate n str ) ) g ( k 3) = fst ( k 3 "") : [ limit ] = fst (3 ( length "" + 1), unwords ( replicate 3 "") ) : [ limit ] = (3 ( length "" + 1) ) : [ limit ] = (3 (0 + 1) ) : [ limit ] = (3 1) : [ limit ] = 3 : [ limit ]
260 FP g :: ( String ( Int, a ) ) [ Int ] g h = fst ( h "") : [ limit ] Partial Application Example k :: Int String ( Int, String ) k n str = ( n ( length str + 1), unwords ( replicate n str ) ) g ( k 3) = fst ( k 3 "") : [ limit ] = fst (3 ( length "" + 1), unwords ( replicate 3 "") ) : [ limit ] = (3 ( length "" + 1) ) : [ limit ] = (3 (0 + 1) ) : [ limit ] = (3 1) : [ limit ] = 3 : [ limit ] = [3, 100]
261 FP Operations on Functions id :: a -> a id x = x identity function (.) :: (b -> c) -> (a -> b) -> (a -> c) function composition (f. g) x = f (g x) flip :: (a -> b -> c) -> (b -> a -> c) flip f x y = f y x curry :: ((a,b) -> c) -> (a -> b -> c) curry g x y = g (x,y) argument swapping currying uncurry :: (a -> b -> c) -> ((a,b) -> c) uncurry f (x,y) = f x y Exercise (necessary!): Copy only the definitions to a sheet of paper, and then infer the types yourself!
262 FP Operator Sections Infix operators are turned into functions by surrounding them with parentheses: This is necessary in type declarations: (+) 2 3 = (+) :: Int -> Int -> Int not the natural type of (+) (:) :: a -> [a] -> [a] (++) :: [a] -> [a] -> [a] It is also possible to supply only one argument (which has to be an atomic expression): (2 +) 3 = = (+ 3 ) 2 (8,3 /) 2.5 = 8.3 / 2.5 = (/ 2.5) 8.3 (7 :) [] = 7 : [] = (: [] ) 7 ((2^17) :) (16:[]) = (2^17) : 16 : [] = (: (16:[])) (2^17)
263 FP Turning Functions into Infix Operators Surrounding a function name by backquotes turns it into an infix operator. Frequently used examples (not the natural types throughout): div, mod, max, min :: Int -> Int -> Int elem :: Int -> [Int] -> Bool 12 div 7 = 1 12 mod 7 = 5 12 max 7 = min 7 = 7 12 elem [1.. 10] = False
264 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null :: [ a ] Bool
265 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] = :: [ a ] Bool
266 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] = null ( x : xs ) = :: [ a ] Bool
267 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] :: [ a ] Bool = True null ( x : xs ) =
268 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] :: [ a ] Bool = True null ( x : xs ) = False
269 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] :: [ a ] Bool = True null ( x : xs ) = False head :: [ a ] a
270 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] :: [ a ] Bool = True null ( x : xs ) = False head :: [ a ] a head ( x : xs ) = x
271 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] :: [ a ] Bool = True null ( x : xs ) = False head :: [ a ] a head ( x : xs ) = x tail :: [ a ] [ a ]
272 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] :: [ a ] Bool = True null ( x : xs ) = False head :: [ a ] a head ( x : xs ) = x tail :: [ a ] [ a ] tail ( x : xs ) = xs
273 FP Defining Functions Over Lists by Pattern Matching Some functions taking lists as arguments can be defined directly via pattern matching: null null [ ] :: [ a ] Bool = True null ( x : xs ) = False head :: [ a ] a head ( x : xs ) = x tail :: [ a ] [ a ] tail ( x : xs ) = xs (head and tail are partial functions both are undefined on the empty list.)
274 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int concat :: [ [ a ] ] [ a ] ( ++ ) :: [ a ] [ a ] [ a ] product :: [ Integer ] Integer sum :: [ Integer ] Integer ( elem ) :: Int [ Int ] Bool
275 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = length ( x : xs ) = concat :: [ [ a ] ] [ a ] concat [ ] = concat ( xs : xss ) = ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ( x : xs ) ++ ys = x elem [ ] = x elem ( y : ys ) = sum [ ] = sum ( x : xs ) = product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
276 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = concat :: [ [ a ] ] [ a ] concat [ ] = concat ( xs : xss ) = ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ( x : xs ) ++ ys = x elem [ ] = x elem ( y : ys ) = sum [ ] = sum ( x : xs ) = product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
277 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = concat ( xs : xss ) = ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ( x : xs ) ++ ys = x elem [ ] = x elem ( y : ys ) = sum [ ] = sum ( x : xs ) = product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
278 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = concat ( xs : xss ) = ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x elem [ ] = x elem ( y : ys ) = sum [ ] = sum ( x : xs ) = product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
279 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = concat ( xs : xss ) = ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x : ( xs ++ ys ) x elem [ ] = x elem ( y : ys ) = sum [ ] = sum ( x : xs ) = product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
280 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = [ ] concat ( xs : xss ) = ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x : ( xs ++ ys ) x elem [ ] = x elem ( y : ys ) = sum [ ] = sum ( x : xs ) = product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
281 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = [ ] concat ( xs : xss ) = xs ++ concat xss ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x : ( xs ++ ys ) x elem [ ] = x elem ( y : ys ) = sum [ ] = sum ( x : xs ) = product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
282 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = [ ] concat ( xs : xss ) = xs ++ concat xss ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x : ( xs ++ ys ) x elem [ ] = x elem ( y : ys ) = sum [ ] = 0 sum ( x : xs ) = product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
283 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = [ ] concat ( xs : xss ) = xs ++ concat xss ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x : ( xs ++ ys ) x elem [ ] = x elem ( y : ys ) = sum [ ] = 0 sum ( x : xs ) = x + sum xs product [ ] = product ( x : xs ) = (All these functions are in the standard prelude.)
284 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = [ ] concat ( xs : xss ) = xs ++ concat xss ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x : ( xs ++ ys ) x elem [ ] = x elem ( y : ys ) = sum [ ] = 0 sum ( x : xs ) = x + sum xs product [ ] = 0 product ( x : xs ) = (All these functions are in the standard prelude.)
285 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = [ ] concat ( xs : xss ) = xs ++ concat xss ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x : ( xs ++ ys ) x elem [ ] = x elem ( y : ys ) = sum [ ] = 0 sum ( x : xs ) = x + sum xs product [ ] = 0 product ( x : xs ) = x product xs (All these functions are in the standard prelude.)
286 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = [ ] concat ( xs : xss ) = xs ++ concat xss ( ++ ) :: [ a ] [ a ] [ a ] [ ] ++ ys = ys ( x : xs ) ++ ys = x : ( xs ++ ys ) x elem [ ] = False x elem ( y : ys ) = sum [ ] = 0 sum ( x : xs ) = x + sum xs product [ ] = 0 product ( x : xs ) = x product xs (All these functions are in the standard prelude.)
287 FP Defining Functions Over Lists by Structural Induction Many functions taking lists as arguments can be defined via structural induction: length :: [ a ] Int length [ ] = 0 length ( x : xs ) = 1 + length xs concat :: [ [ a ] ] [ a ] concat [ ] = [ ] concat ( xs : xss ) = xs ++ concat xss ( ++ ) :: [ a ] [ a ] [ a ] sum [ ] = 0 [ ] ++ ys = ys sum ( x : xs ) = x + sum xs ( x : xs ) ++ ys = x : ( xs ++ ys ) product [ ] = 0 product ( x : xs ) = x product xs x elem [ ] = False x elem ( y : ys ) = x y x elem ys (All these functions are in the standard prelude.)
288 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions
289 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!"
290 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!" If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ]
291 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!" If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3]
292 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!" If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3] = take_while ( < 5) (1 : 2 : 3 : [ ] )
293 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!" If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3] = take_while ( < 5) (1 : 2 : 3 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : [ ] )
294 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!" If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3] = take_while ( < 5) (1 : 2 : 3 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : [ ] )
295 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!" If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3] = take_while ( < 5) (1 : 2 : 3 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) [ ]
296 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!" If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3] = take_while ( < 5) (1 : 2 : 3 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) [ ] = 1 : 2 : 3 : [ ]
297 FP sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 Guarded Definitions choose :: Ord a ( a, b ) ( a, b ) b choose ( x, v ) ( y, w ) x > y = v x < y = w otherwise = error "I cannot decide!" If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3] = take_while ( < 5) (1 : 2 : 3 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) [ ] = 1 : 2 : 3 : [ ] = [1, 2, 3]
298 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ]
299 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6]
300 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] )
301 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] )
302 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] )
303 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) (2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] )
304 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) (2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : take_while ( < 5) (3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] )
305 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) (2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : take_while ( < 5) (3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : take_while ( < 5) (4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] )
306 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) (2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : take_while ( < 5) (3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : take_while ( < 5) (4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : take_while ( < 5) (3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] )
307 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) (2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : take_while ( < 5) (3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : take_while ( < 5) (4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : take_while ( < 5) (3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : take_while ( < 5) (4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] )
308 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) (2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : take_while ( < 5) (3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : take_while ( < 5) (4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : take_while ( < 5) (3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : take_while ( < 5) (4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : take_while ( < 5) (5 : 4 : 3 : 4 : 5 : 6 : [ ] )
309 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) (2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : take_while ( < 5) (3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : take_while ( < 5) (4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : take_while ( < 5) (3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : take_while ( < 5) (4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : take_while ( < 5) (5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : [ ]
310 FP Guarded Definitions Fall-Through If no guard succeeds, the next pattern is tried: take_while p ( x : xs ) p x = x : take_while p xs take_while p xs = [ ] take_while ( < 5) [1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6] = take_while ( < 5) (1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : take_while ( < 5) (2 : 3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : take_while ( < 5) (3 : 2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : take_while ( < 5) (2 : 3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : take_while ( < 5) (3 : 4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : take_while ( < 5) (4 : 3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : take_while ( < 5) (3 : 4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : take_while ( < 5) (4 : 5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : take_while ( < 5) (5 : 4 : 3 : 4 : 5 : 6 : [ ] ) = 1 : 2 : 3 : 2 : 3 : 4 : 3 : 4 : [ ] = [1, 2, 3, 2, 3, 4, 3, 4]
311 FP case Expressions sign x = case compare x 0 of GT -> 1 EQ -> 0 LT -> -1
312 FP case Expressions sign x = case compare x 0 of GT -> 1 EQ -> 0 LT -> -1 The prelude datatype Ordering has three elements and is used mostly as result type of the prelude function compare: data Ordering = LT EQ GT
313 FP case Expressions sign x = case compare x 0 of GT -> 1 EQ -> 0 LT -> -1 The prelude datatype Ordering has three elements and is used mostly as result type of the prelude function compare: data Ordering = LT EQ GT compare :: Ord a a a Ordering
314 FP case Expressions sign x = case compare x 0 of GT -> 1 EQ -> 0 LT -> -1 The prelude datatype Ordering has three elements and is used mostly as result type of the prelude function compare: data Ordering = LT EQ GT compare :: Ord a a a Ordering Another example: choose ( x, v ) ( y, w ) = case compare x y of GT v LT w EQ error "I cannot decide!"
315 FP if then else and case Expressions The type Bool can be considered as a two-element enumeration type: data Bool = False True
316 FP if then else and case Expressions The type Bool can be considered as a two-element enumeration type: data Bool = False True Conditional expressions are syntactic sugar for case expressions over Bool: if condition then expr1 else expr2 case condition of True expr1 False expr2
317 FP if then else and case Expressions The type Bool can be considered as a two-element enumeration type: data Bool = False True Conditional expressions are syntactic sugar for case expressions over Bool: if condition then expr1 else expr2 case condition of True expr1 False expr2 Two ways of defining functions: Pattern Matching not True = False not False = True case not b = case b of True False False True
318 FP case Expressions are Anonymous Pattern Matching commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ case xs of [ ] [ ] _ ", " : commawords xs
319 FP case Expressions are Anonymous Pattern Matching commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ case xs of [ ] [ ] _ ", " : commawords xs Every use of a case expression can be transformed into the use of an auxiliary function defined by pattern matching
320 FP case Expressions are Anonymous Pattern Matching commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ case xs of [ ] [ ] _ ", " : commawords xs Every use of a case expression can be transformed into the use of an auxiliary function defined by pattern matching: commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ commawordsaux xs
321 FP case Expressions are Anonymous Pattern Matching commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ case xs of [ ] [ ] _ ", " : commawords xs Every use of a case expression can be transformed into the use of an auxiliary function defined by pattern matching: commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ commawordsaux xs commawordsaux [ ] = [ ] commawordsaux xs = ", " : commawords xs
322 FP where Clauses
323 FP where Clauses If an auxiliary definition is used only locally, it should be inside a local definition
324 FP where Clauses If an auxiliary definition is used only locally, it should be inside a local definition, e.g.: commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ commawordsaux xs where commawordsaux [ ] = [ ] commawordsaux xs = ", " : commawords xs
325 FP where Clauses If an auxiliary definition is used only locally, it should be inside a local definition, e.g.: commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ commawordsaux xs where commawordsaux [ ] = [ ] commawordsaux xs = ", " : commawords xs where clauses are visible only within their enclosing clause, here commawords ( x : xs ) =
326 FP where Clauses If an auxiliary definition is used only locally, it should be inside a local definition, e.g.: commawords :: [ String ] String commawords [ ] = [ ] commawords ( x : xs ) = x ++ commawordsaux xs where commawordsaux [ ] = [ ] commawordsaux xs = ", " : commawords xs where clauses are visible only within their enclosing clause, here commawords ( x : xs ) = where clauses are visible within all guards: f x y y > z = y == z = y < z = where z = x * x
327 FP let Expressions Local definitions can also be part of expressions: f k n = let m = k mod n in if m == 0 then n else f n m
328 FP let Expressions Local definitions can also be part of expressions: f k n = let m = k mod n in if m == 0 then n else f n m h x y = let x2 = x * x y2 = y * y in sqrt (x2 + y2)
329 FP let Expressions Local definitions can also be part of expressions: f k n = let m = k mod n in if m == 0 then n else f n m h x y = let x2 = x * x y2 = y * y in sqrt (x2 + y2) Definitions can use pattern bindings: g k n = let (d,m) = divmod k n in if d == 0 then [m] else g d n ++ [m]
330 FP let Expressions Local definitions can also be part of expressions: f k n = let m = k mod n in if m == 0 then n else f n m h x y = let x2 = x * x y2 = y * y in sqrt (x2 + y2) Definitions can use pattern bindings: g k n = let (d,m) = divmod k n in if d == 0 then [m] else g d n ++ [m] Guards, let and where bindings, and case cases all are layout sensitive!
331 FP let or where?
332 FP let or where? let bindings in expression is an expression
333 FP let or where? let bindings in expression is an expression fname patterns guardedrhss where bindings is a clause that is part of a definition
334 FP let or where? let bindings in expression is an expression fname patterns guardedrhss where bindings is a clause that is part of a definition (where clauses can also modify case cases)
335 FP let or where? let bindings in expression is an expression fname patterns guardedrhss where bindings is a clause that is part of a definition (where clauses can also modify case cases) Frequently, the choice between let and where is a matter of style: where clauses result in a top-down presentation let expressions lend themselves also to bottom-up presentations
336 FP Some Prelude Functions Elementary List Access head head (x:_) last last [x] last (_:xs) tail tail (_:xs) :: [a] -> a = x :: [a] -> a = x = last xs :: [a] -> [a] = xs init :: [a] -> [a] init [x] = [] init (x:xs) = x : init xs null null [] null (_:_) :: [a] -> Bool = True = False
337 FP Some Prelude Functions List Indexing length :: [a] -> Int length = foldl (\n _ -> n + 1) 0 (!!) :: [b] -> Int -> b (x:_)!! 0 = x (_:xs)!! n n>0 = xs!! (n-1) (_:_)!! _ = error "PreludeList.!!: negative index" []!! _ = error "PreludeList.!!: index too large"
338 FP Some Prelude Functions Positional List Splitting take :: Int -> [a] -> [a] take 0 _ = [] take _ [] = [] take n (x:xs) n>0 = x : take (n-1) xs take = error "take: negative argument" drop :: Int -> [a] -> [a] drop 0 xs = xs drop _ [] = [] drop n (_:xs) n>0 = drop (n-1) xs drop = error "drop: negative argument" splitat :: Int -> [a] -> ([a], [a]) splitat 0 xs = ([],xs) splitat _ [] = ([],[]) splitat n (x:xs) n>0 = (x:xs,xs ) where (xs,xs ) = splitat (n-1) xs splitat = error "splitat: negative argument"
339 FP Some Prelude Functions Concatenation, Iteration (++) :: [a] -> [a] -> [a] [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys) concat :: [[a]] -> [a] concat = foldr (++) [] iterate :: (a -> a) -> a -> [a] iterate f x = x : iterate f (f x) repeat :: a -> [a] repeat x = xs where xs = x:xs { repeat x = x : repeat x } for understanding replicate :: Int -> a -> [a] replicate n x = take n (repeat x) cycle :: [a] -> [a] cycle xs = xs where xs = xs ++ xs
340 FP Separation of Concerns: Generation and Consumption
341 FP Separation of Concerns: Generation and Consumption replicate 3!
342 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate
343 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat
344 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii)
345 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction
346 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction =! : take 2 (! : repeat! ) repeat
347 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction =! : take 2 (! : repeat! ) repeat =! :! : take (2-1) (repeat! ) take (iii)
348 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction =! : take 2 (! : repeat! ) repeat =! :! : take (2-1) (repeat! ) take (iii) =! :! : take 1 (repeat! ) subtraction
349 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction =! : take 2 (! : repeat! ) repeat =! :! : take (2-1) (repeat! ) take (iii) =! :! : take 1 (repeat! ) subtraction =! :! : take 1 (! : repeat! ) repeat
350 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction =! : take 2 (! : repeat! ) repeat =! :! : take (2-1) (repeat! ) take (iii) =! :! : take 1 (repeat! ) subtraction =! :! : take 1 (! : repeat! ) repeat =! :! :! : take (1-1) (repeat! ) take (iii)
351 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction =! : take 2 (! : repeat! ) repeat =! :! : take (2-1) (repeat! ) take (iii) =! :! : take 1 (repeat! ) subtraction =! :! : take 1 (! : repeat! ) repeat =! :! :! : take (1-1) (repeat! ) take (iii) =! :! :! : take 0 (repeat! ) subtraction
352 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction =! : take 2 (! : repeat! ) repeat =! :! : take (2-1) (repeat! ) take (iii) =! :! : take 1 (repeat! ) subtraction =! :! : take 1 (! : repeat! ) repeat =! :! :! : take (1-1) (repeat! ) take (iii) =! :! :! : take 0 (repeat! ) subtraction =! :! :! : [] take (i)
353 FP Separation of Concerns: Generation and Consumption replicate 3! = take 3 (repeat! ) replicate = take 3 (! : repeat! ) repeat =! : take (3-1) (repeat! ) take (iii) =! : take 2 (repeat! ) subtraction =! : take 2 (! : repeat! ) repeat =! :! : take (2-1) (repeat! ) take (iii) =! :! : take 1 (repeat! ) subtraction =! :! : take 1 (! : repeat! ) repeat =! :! :! : take (1-1) (repeat! ) take (iii) =! :! :! : take 0 (repeat! ) subtraction =! :! :! : [] take (i) = "!!!"
354 FP What We Have Seen So Far
355 FP Functional programming: What We Have Seen So Far
356 FP What We Have Seen So Far Functional programming: Higher-order functions
357 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results
358 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems:
359 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors
360 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism
361 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables)
362 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference
363 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules:
364 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules: juxtaposition as operator
365 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules: juxtaposition as operator, associate to the left/right
366 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules: juxtaposition as operator, associate to the left/right Argument passing:
367 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules: juxtaposition as operator, associate to the left/right Argument passing: not by value or reference
368 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules: juxtaposition as operator, associate to the left/right Argument passing: not by value or reference, but by name
369 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules: juxtaposition as operator, associate to the left/right Argument passing: not by value or reference, but by name Powerful datatypes with simple interface:
370 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules: juxtaposition as operator, associate to the left/right Argument passing: not by value or reference, but by name Powerful datatypes with simple interface: Integer, lists, lists of lists of
371 FP What We Have Seen So Far Functional programming: Higher-order functions, functions as arguments and results Type systems: type constants and type constructors, parametric polymorphism (type variables), type inference Operator precedence rules: juxtaposition as operator, associate to the left/right Argument passing: not by value or reference, but by name Powerful datatypes with simple interface: Integer, lists, lists of lists of Non-local control (evaluation on demand): modularity (e.g., generate / prune)
372 FP Some Prelude Functions List Splitting with Predicates takewhile :: (a -> Bool) -> [a] -> [a] takewhile p [] = [] takewhile p (x:xs) p x = x : takewhile p xs otherwise = [] dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs span, break :: (a -> Bool) -> [a] -> ([a],[a]) span p [] = ([],[]) span p xs@(x:xs ) p x = let (ys,zs) = span p xs in (x:ys,zs) otherwise = ([],xs) break p = span (not. p)
373 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs
374 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]:
375 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]: p = xs = x = xs =
376 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]: p = ( < 5) xs = x = xs =
377 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]: p = ( < 5) xs = [1,2,3] x = xs =
378 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]: p = ( < 5) xs = [1,2,3] x = 1 xs =
379 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]: p = ( < 5) xs = [1,2,3] x = 1 xs = [2,3]
380 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]: p = ( < 5) xs = [1,2,3] x = 1 xs = [2,3] p x = ( < 5) 1 = 1 < 5 = True
381 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]: p = ( < 5) xs = [1,2,3] x = 1 xs = [2,3] p x = ( < 5) 1 = 1 < 5 = True Therefore: dropwhile ( < 5) [1,2,3] =
382 FP as-patterns dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 1,2,3]: p = ( < 5) xs = [1,2,3] x = 1 xs = [2,3] p x = ( < 5) 1 = 1 < 5 = True Therefore: dropwhile ( < 5) [1,2,3] = dropwhile ( < 5) [2,3]
383 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]:
384 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]: p = xs = x = xs =
385 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]: p = ( < 5) xs = x = xs =
386 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]: p = ( < 5) xs = [5,4,3] x = xs =
387 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]: p = ( < 5) xs = [5,4,3] x = 5 xs =
388 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]: p = ( < 5) xs = [5,4,3] x = 5 xs = [4,3]
389 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]: p = ( < 5) xs = [5,4,3] x = 5 xs = [4,3] p x = ( < 5) 5 = 5 < 5 = False
390 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]: p = ( < 5) xs = [5,4,3] x = 5 xs = [4,3] p x = ( < 5) 5 = 5 < 5 = False Therefore: dropwhile ( < 5) [5,4,3] =
391 FP as-patterns 2 dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs Consider matching of the third clause against dropwhile ( < 5) [ 5,4,3]: p = ( < 5) xs = [5,4,3] x = 5 xs = [4,3] p x = ( < 5) 5 = 5 < 5 = False Therefore: dropwhile ( < 5) [5,4,3] = [5,4,3]
392 FP Some Prelude Functions List Splitting with Predicates takewhile :: (a -> Bool) -> [a] -> [a] takewhile p [] = [] takewhile p (x:xs) p x = x : takewhile p xs otherwise = [] dropwhile :: (a -> Bool) -> [a] -> [a] dropwhile p [] = [] dropwhile p xs@(x:xs ) p x = dropwhile p xs otherwise = xs span, break :: (a -> Bool) -> [a] -> ([a],[a]) span p [] = ([],[]) span p xs@(x:xs ) p x = let (ys,zs) = span p xs in (x:ys,zs) otherwise = ([],xs) break p = span (not. p)
393 FP Some Prelude Functions Text Processing lines :: String -> [String] lines "" = [] lines s = let (l,s ) = break ( \n ==) s in l : case s of [] -> [] (_:s ) -> lines s words words s :: String -> [String] = case dropwhile isspace s of "" -> [] s -> w : words s where (w,s ) = break isspace s unlines :: [String] -> String unlines [] = [] unlines (l:ls) = l ++ \n : unlines ls unwords :: [String] -> String unwords [] = "" unwords [w] = w unwords (w:ws) = w ++ : unwords ws
394 FP map and filter map :: (a -> b) -> ([a] -> [b]) map f [] = [] map f (x:xs) = f x : map f xs
395 FP map and filter map :: (a -> b) -> ([a] -> [b]) map f [] = [] map f (x:xs) = f x : map f xs filter :: (a -> Bool) -> ([a] -> [a]) filter p [] = [] filter p (x : xs) = if p x then x : rest else rest where rest = filter p xs
396 FP map and filter map :: (a -> b) -> ([a] -> [b]) map f [] = [] map f (x:xs) = f x : map f xs filter :: (a -> Bool) -> ([a] -> [a]) filter p [] = [] filter p (x : xs) = if p x then x : rest else rest where rest = filter p xs These functions could also be defined via list comprehension: map f xs = [ f x x <- xs ]
397 FP map and filter map :: (a -> b) -> ([a] -> [b]) map f [] = [] map f (x:xs) = f x : map f xs filter :: (a -> Bool) -> ([a] -> [a]) filter p [] = [] filter p (x : xs) = if p x then x : rest else rest where rest = filter p xs These functions could also be defined via list comprehension: map f xs = [ f x x <- xs ] filter p xs = [ x x <- xs, p x ]
398 FP map and filter map :: (a -> b) -> ([a] -> [b]) map f [] = [] map f (x:xs) = f x : map f xs filter :: (a -> Bool) -> ([a] -> [a]) filter p [] = [] filter p (x : xs) = if p x then x : rest else rest where rest = filter p xs These functions could also be defined via list comprehension: map f xs = [ f x x <- xs ] filter p xs = [ x x <- xs, p x ] Examples: map (7 *) [1.. 6] = [7, 14, 21, 28, 35, 42]
399 FP map and filter map :: (a -> b) -> ([a] -> [b]) map f [] = [] map f (x:xs) = f x : map f xs filter :: (a -> Bool) -> ([a] -> [a]) filter p [] = [] filter p (x : xs) = if p x then x : rest else rest where rest = filter p xs These functions could also be defined via list comprehension: map f xs = [ f x x <- xs ] filter p xs = [ x x <- xs, p x ] Examples: map (7 *) [1.. 6] = [7, 14, 21, 28, 35, 42] filter even [1.. 6] = [2, 4, 6]
400 FP foldr1 foldr1 foldr1 ( ) [x] :: (a -> a -> a) -> [a] -> a = x foldr1 ( ) (x:xs) = x (foldr1 ( ) xs)
401 FP foldr1 foldr1 foldr1 ( ) [x] :: (a -> a -> a) -> [a] -> a = x foldr1 ( ) (x:xs) = x (foldr1 ( ) xs) foldr1 ( ) [x 1, x 2, x 3, x 4, x 5 ]
402 FP foldr1 foldr1 foldr1 ( ) [x] :: (a -> a -> a) -> [a] -> a = x foldr1 ( ) (x:xs) = x (foldr1 ( ) xs) foldr1 ( ) [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr1 ( ) [x 2, x 3, x 4, x 5 ])
403 FP foldr1 foldr1 foldr1 ( ) [x] :: (a -> a -> a) -> [a] -> a = x foldr1 ( ) (x:xs) = x (foldr1 ( ) xs) foldr1 ( ) [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr1 ( ) [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr1 ( ) [x 3, x 4, x 5 ]))
404 FP foldr1 foldr1 foldr1 ( ) [x] :: (a -> a -> a) -> [a] -> a = x foldr1 ( ) (x:xs) = x (foldr1 ( ) xs) foldr1 ( ) [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr1 ( ) [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr1 ( ) [x 3, x 4, x 5 ])) = x 1 (x 2 (x 3 (foldr1 ( ) [x 4, x 5 ])))
405 FP foldr1 foldr1 foldr1 ( ) [x] :: (a -> a -> a) -> [a] -> a = x foldr1 ( ) (x:xs) = x (foldr1 ( ) xs) foldr1 ( ) [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr1 ( ) [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr1 ( ) [x 3, x 4, x 5 ])) = x 1 (x 2 (x 3 (foldr1 ( ) [x 4, x 5 ]))) = x 1 (x 2 (x 3 (x 4 (foldr1 ( ) [x 5 ]))))
406 FP foldr1 foldr1 foldr1 ( ) [x] :: (a -> a -> a) -> [a] -> a = x foldr1 ( ) (x:xs) = x (foldr1 ( ) xs) foldr1 ( ) [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr1 ( ) [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr1 ( ) [x 3, x 4, x 5 ])) = x 1 (x 2 (x 3 (foldr1 ( ) [x 4, x 5 ]))) = x 1 (x 2 (x 3 (x 4 (foldr1 ( ) [x 5 ])))) = x 1 (x 2 (x 3 (x 4 x 5 )))
407 FP foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr ( ) z [] = z foldr ( ) z (x:xs) = x (foldr ( ) z xs)
408 FP foldrx foldrx :: ( a b b ) b [ a ] b foldrx ( ) z [ ] = z foldrx ( ) z ( x : xs ) = x ( foldrx ( ) z xs )
409 FP foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr ( ) z [] = z foldr ( ) z (x:xs) = x (foldr ( ) z xs) foldr ( ) z [x 1, x 2, x 3, x 4, x 5 ]
410 FP foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr ( ) z [] = z foldr ( ) z (x:xs) = x (foldr ( ) z xs) foldr ( ) z [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr ( ) z [x 2, x 3, x 4, x 5 ])
411 FP foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr ( ) z [] = z foldr ( ) z (x:xs) = x (foldr ( ) z xs) foldr ( ) z [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr ( ) z [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr ( ) z [x 3, x 4, x 5 ]))
412 FP foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr ( ) z [] = z foldr ( ) z (x:xs) = x (foldr ( ) z xs) foldr ( ) z [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr ( ) z [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr ( ) z [x 3, x 4, x 5 ])) = x 1 (x 2 (x 3 (foldr ( ) z [x 4, x 5 ])))
413 FP foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr ( ) z [] = z foldr ( ) z (x:xs) = x (foldr ( ) z xs) foldr ( ) z [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr ( ) z [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr ( ) z [x 3, x 4, x 5 ])) = x 1 (x 2 (x 3 (foldr ( ) z [x 4, x 5 ]))) = x 1 (x 2 (x 3 (x 4 (foldr ( ) z [x 5 ]))))
414 FP foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr ( ) z [] = z foldr ( ) z (x:xs) = x (foldr ( ) z xs) foldr ( ) z [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr ( ) z [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr ( ) z [x 3, x 4, x 5 ])) = x 1 (x 2 (x 3 (foldr ( ) z [x 4, x 5 ]))) = x 1 (x 2 (x 3 (x 4 (foldr ( ) z [x 5 ])))) = x 1 (x 2 (x 3 (x 4 (x 5 (foldr ( ) z [])))))
415 FP foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr ( ) z [] = z foldr ( ) z (x:xs) = x (foldr ( ) z xs) foldr ( ) z [x 1, x 2, x 3, x 4, x 5 ] = x 1 (foldr ( ) z [x 2, x 3, x 4, x 5 ]) = x 1 (x 2 (foldr ( ) z [x 3, x 4, x 5 ])) = x 1 (x 2 (x 3 (foldr ( ) z [x 4, x 5 ]))) = x 1 (x 2 (x 3 (x 4 (foldr ( ) z [x 5 ])))) = x 1 (x 2 (x 3 (x 4 (x 5 (foldr ( ) z []))))) = x 1 (x 2 (x 3 (x 4 (x 5 z))))
416 FP List Folding foldr foldr f z [] foldr f z (x:xs) foldr1 foldr1 f [x] foldr1 f (x:xs) foldl foldl f z [] foldl f z (x:xs) foldl1 foldl1 f (x:xs) :: (a -> b -> b) -> b -> [a] -> b = z = f x (foldr f z xs) :: (a -> a -> a) -> [a] -> a = x = f x (foldr1 f xs) :: (a -> b -> a) -> a -> [b] -> a = z = foldl f (f z x) xs :: (a -> a -> a) -> [a] -> a = foldl f x xs
417 FP A simple definition: limit = 10 ^ 2 Unfolding Definitions
418 FP A simple definition: limit = 10 ^ 2 Expanding this definition: 4 ( limit + 1) Unfolding Definitions
419 FP A simple definition: limit = 10 ^ 2 Expanding this definition: 4 * (limit + 1) = 4 * ((10 ^ 2) + 1) Unfolding Definitions
420 FP A simple definition: limit = 10 ^ 2 Expanding this definition: 4 * (limit + 1) = 4 * ((10 ^ 2) + 1) = Unfolding Definitions
421 FP A simple definition: limit = 10 ^ 2 Expanding this definition: 4 * (limit + 1) = 4 * ((10 ^ 2) + 1) = Unfolding Definitions Another definition: concat = foldr ( ++ ) [ ]
422 FP A simple definition: limit = 10 ^ 2 Expanding this definition: 4 * (limit + 1) = 4 * ((10 ^ 2) + 1) = Unfolding Definitions Another definition: concat = foldr ( ++ ) [ ] Expanding this definition: concat [ [1,2,3], [4,5] ]
423 FP A simple definition: limit = 10 ^ 2 Expanding this definition: 4 * (limit + 1) = 4 * ((10 ^ 2) + 1) = Unfolding Definitions Another definition: concat = foldr ( ++ ) [ ] Expanding this definition: concat [ [1,2,3], [4,5] ] = ( foldr ( ++ ) [ ] ) [ [1,2,3], [4,5] ] =
424 FP Enumeration Type Definitions data Bool = False True deriving ( Eq, Ord, Read, Show ) data Ordering = LT EQ GT deriving ( Eq, Ord, Read, Show ) data Suit = Diamonds Hearts Spades Clubs deriving ( Eq, Ord ) Pattern matching: not False = True not True = False lexicalcombineordering :: Ordering Ordering Ordering lexicalcombineordering LT _ = LT lexicalcombineordering EQ x = x lexicalcombineordering GT _ = GT
425 FP Simple data Type Definitions data Point = Pt Int Int deriving ( Eq ) screen coordinates data Transport = Feet Bike Train Int price in cent This defines at the same time data constructors: Pt :: Int Int Point Feet :: Transport Bike :: Transport Train :: Int Transport Pattern matching: addpt ( Pt x1 y1) ( Pt x2 y2 ) = Pt ( x1 + x2 ) ( y1 + y2 ) cost Feet = 0 cost Bike = 0 cost ( Train Int ) = Int
426 FP Simple Polymorphic data Type Definitions The prelude type constructors Maybe, Either, Complex are defined as follows: data Maybe a = Nothing Just a deriving ( Eq, Ord, Read, Show ) data Either a b = Left a Right b data Complex r = r :+ r deriving ( Eq, Read, Show ) This defines at the same time data constructors: Nothing :: Maybe a Just :: a Maybe a Left :: a Either a b Right :: b Either a b ( :+ ) :: r r Complex r
Functional Programming. Functional Programming Languages. Chapter 14. Introduction
Functional Programming Languages Chapter 14 Introduction Functional programming paradigm History Features and concepts Examples: Lisp ML 1 2 Functional Programming Functional Programming Languages The
Chapter 15 Functional Programming Languages
Chapter 15 Functional Programming Languages Introduction - The design of the imperative languages is based directly on the von Neumann architecture Efficiency (at least at first) is the primary concern,
CSE 307: Principles of Programming Languages
Course Organization Introduction CSE 307: Principles of Programming Languages Spring 2015 R. Sekar Course Organization Introduction 1 / 34 Topics 1. Course Organization Info and Support Course Description
Lecture 1: Introduction
Programming Languages Lecture 1: Introduction Benjamin J. Keller Department of Computer Science, Virginia Tech Programming Languages Lecture 1 Introduction 2 Lecture Outline Preview History of Programming
CSCI 3136 Principles of Programming Languages
CSCI 3136 Principles of Programming Languages Faculty of Computer Science Dalhousie University Winter 2013 CSCI 3136 Principles of Programming Languages Faculty of Computer Science Dalhousie University
High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)
High-Level Programming Languages Nell Dale & John Lewis (adaptation by Michael Goldwasser) Low-Level Languages What are disadvantages of low-level languages? (e.g., machine code or assembly code) Programming
Chapter 7: Functional Programming Languages
Chapter 7: Functional Programming Languages Aarne Ranta Slides for the book Implementing Programming Languages. An Introduction to Compilers and Interpreters, College Publications, 2012. Fun: a language
1.1 WHAT IS A PROGRAMMING LANGUAGE?
1 INTRODUCTION 1.1 What is a Programming Language? 1.2 Abstractions in Programming Languages 1.3 Computational Paradigms 1.4 Language Definition 1.5 Language Translation 1.6 Language Design How we communicate
Moving from CS 61A Scheme to CS 61B Java
Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you
The countdown problem
JFP 12 (6): 609 616, November 2002. c 2002 Cambridge University Press DOI: 10.1017/S0956796801004300 Printed in the United Kingdom 609 F U N C T I O N A L P E A R L The countdown problem GRAHAM HUTTON
VB.NET Programming Fundamentals
Chapter 3 Objectives Programming Fundamentals In this chapter, you will: Learn about the programming language Write a module definition Use variables and data types Compute with Write decision-making statements
CSC 272 - Software II: Principles of Programming Languages
CSC 272 - Software II: Principles of Programming Languages Lecture 1 - An Introduction What is a Programming Language? A programming language is a notational system for describing computation in machine-readable
1 Introduction. 2 An Interpreter. 2.1 Handling Source Code
1 Introduction The purpose of this assignment is to write an interpreter for a small subset of the Lisp programming language. The interpreter should be able to perform simple arithmetic and comparisons
History OOP languages Year Language 1967 Simula-67 1983 Smalltalk
History OOP languages Intro 1 Year Language reported dates vary for some languages... design Vs delievered 1957 Fortran High level programming language 1958 Lisp 1959 Cobol 1960 Algol Structured Programming
Functional Programming in C++11
Functional Programming in C++11 science + computing ag IT-Dienstleistungen und Software für anspruchsvolle Rechnernetze Tübingen München Berlin Düsseldorf An Overview Programming in a functional style
Chapter 1. Dr. Chris Irwin Davis Email: [email protected] Phone: (972) 883-3574 Office: ECSS 4.705. CS-4337 Organization of Programming Languages
Chapter 1 CS-4337 Organization of Programming Languages Dr. Chris Irwin Davis Email: [email protected] Phone: (972) 883-3574 Office: ECSS 4.705 Chapter 1 Topics Reasons for Studying Concepts of Programming
Programming Languages
Programming Languages Qing Yi Course web site: www.cs.utsa.edu/~qingyi/cs3723 cs3723 1 A little about myself Qing Yi Ph.D. Rice University, USA. Assistant Professor, Department of Computer Science Office:
How To Program In Scheme (Prolog)
The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction Next up: Numeric operators, REPL, quotes, functions, conditionals Types and values
Concepts of Programming Languages: A Unified Approach. Karl Abrahamson
Concepts of Programming Languages: A Unified Approach Karl Abrahamson August 2013 2 Copyright (c) 2013 Karl Abrahamson. Contents 1 Introduction to Programming Languages 11 1.1 Programming languages.............................
Announcements FORTRAN ALGOL COBOL. Simula & Smalltalk. Programming Languages
Announcements Programming Languages! Monday evening GBA section has been shut down " If you were assigned to this section, please find a different section " If you cannot attend a different section, please
Course Goal CMSC 330: Organization of Programming Languages. Studying Programming Languages. All Languages Are (Kind of) Equivalent.
Course Goal CMSC 330: Organization of Programming Languages Introduction Instructors: Michael Hicks and Anwar Mamat Learn how programming languages work Broaden your language horizons Different programming
Programming Languages in Artificial Intelligence
Programming Languages in Artificial Intelligence Günter Neumann, German Research Center for Artificial Intelligence (LT Lab, DFKI) I. AI programming languages II. Functional programming III. Functional
[Refer Slide Time: 05:10]
Principles of Programming Languages Prof: S. Arun Kumar Department of Computer Science and Engineering Indian Institute of Technology Delhi Lecture no 7 Lecture Title: Syntactic Classes Welcome to lecture
CSE 130 Programming Language Principles & Paradigms
CSE 130 Programming Language Principles & Paradigms Thomas A. Powell [email protected] Housekeeping Syllabus review Direct class page link http://www.pint.com/classes/cse130 Updated syllabus, notes, homework
Principles of Programming Languages Topic: Introduction Professor Louis Steinberg
Principles of Programming Languages Topic: Introduction Professor Louis Steinberg CS 314, LS,LTM: L1: Introduction 1 Contacts Prof. Louis Steinberg lou @ cs.rutgers.edu x5-3581 401 Hill TA: to be announced
Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz 2007 03 16
Advanced compiler construction Michel Schinz 2007 03 16 General course information Teacher & assistant Course goals Teacher: Michel Schinz [email protected] Assistant: Iulian Dragos INR 321, 368 64
Semester Review. CSC 301, Fall 2015
Semester Review CSC 301, Fall 2015 Programming Language Classes There are many different programming language classes, but four classes or paradigms stand out:! Imperative Languages! assignment and iteration!
Object Oriented Software Design
Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 14, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction
1998. (R. Bird and P. Wadler, Introduction to Functional Programming, Prentice
Mathematical Structures in Programs 15 Algebra) The Shorter Oxford English Dictionary): the reunion of broken parts a calculus of symbols combined according to defined laws Haskell 3 4 Richard Bird. Introduction
Integrating Formal Models into the Programming Languages Course
Integrating Formal Models into the Programming Languages Course Allen B. Tucker Robert E. Noonan Computer Science Department Computer Science Department Bowdoin College College of William and Mary Brunswick,
Chapter 6: Programming Languages
Chapter 6: Programming Languages Computer Science: An Overview Eleventh Edition by J. Glenn Brookshear Copyright 2012 Pearson Education, Inc. Chapter 6: Programming Languages 6.1 Historical Perspective
The Clean programming language. Group 25, Jingui Li, Daren Tuzi
The Clean programming language Group 25, Jingui Li, Daren Tuzi The Clean programming language Overview The Clean programming language first appeared in 1987 and is still being further developed. It was
Anatomy of Programming Languages. William R. Cook
Anatomy of Programming Languages William R. Cook Copyright (C) 2013 2 Chapter 1 Preliminaries Preface What? This document is a series of notes about programming languages, originally written for students
POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful
POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful RALF HINZE Institute of Information and Computing Sciences Utrecht University Email: [email protected] Homepage: http://www.cs.uu.nl/~ralf/
by Mario Fusco [email protected] twitter: @mariofusco Monadic
by Mario Fusco [email protected] twitter: @mariofusco Monadic Imperative languages Java C# C / C++ Fortran Add abstractions Scala F# Subtract abstractions Hybrid languages Algol Lisp ML Haskell Functional
7.1 Our Current Model
Chapter 7 The Stack In this chapter we examine what is arguably the most important abstract data type in computer science, the stack. We will see that the stack ADT and its implementation are very simple.
Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!
Programming Language Rankings Lecture 15: Type Inference, polymorphism & Type Classes CSC 131 Fall, 2014 Kim Bruce Top Combined Tiobe Index 1. JavaScript (+1) 2. Java (-1) 3. PHP 4. C# (+2) 5. Python (-1)
Chapter 13 Computer Programs and Programming Languages. Discovering Computers 2012. Your Interactive Guide to the Digital World
Chapter 13 Computer Programs and Programming Languages Discovering Computers 2012 Your Interactive Guide to the Digital World Objectives Overview Differentiate between machine and assembly languages Identify
Introduction to Software Paradigms & Procedural Programming Paradigm
Introduction & Procedural Programming Sample Courseware Introduction to Software Paradigms & Procedural Programming Paradigm This Lesson introduces main terminology to be used in the whole course. Thus,
McGraw-Hill The McGraw-Hill Companies, Inc., 20 1. 01 0
1.1 McGraw-Hill The McGraw-Hill Companies, Inc., 2000 Objectives: To describe the evolution of programming languages from machine language to high-level languages. To understand how a program in a high-level
Object Oriented Software Design
Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 28, 2010 G. Lipari (Scuola Superiore Sant Anna) Introduction
Cedalion A Language Oriented Programming Language (Extended Abstract)
Cedalion A Language Oriented Programming Language (Extended Abstract) David H. Lorenz Boaz Rosenan The Open University of Israel Abstract Implementations of language oriented programming (LOP) are typically
Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages
ICOM 4036 Programming Languages Preliminaries Dr. Amirhossein Chinaei Dept. of Electrical & Computer Engineering UPRM Spring 2010 Language Evaluation Criteria Readability: the ease with which programs
Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.
Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to
programming languages, programming language standards and compiler validation
Software Quality Issues when choosing a Programming Language C.J.Burgess Department of Computer Science, University of Bristol, Bristol, BS8 1TR, England Abstract For high quality software, an important
Programming Languages CIS 443
Course Objectives Programming Languages CIS 443 0.1 Lexical analysis Syntax Semantics Functional programming Variable lifetime and scoping Parameter passing Object-oriented programming Continuations Exception
Testing and Tracing Lazy Functional Programs using QuickCheck and Hat
Testing and Tracing Lazy Functional Programs using QuickCheck and Hat Koen Claessen 1, Colin Runciman 2, Olaf Chitil 2, John Hughes 1, and Malcolm Wallace 2 1 Chalmers University of Technology, Sweden
Languages september 12, 2015 Éric Lévénez 1999-2015 <http://www.levenez.com/lang/> FORTRAN III end-1958 FORTRAN II 1957. FORTRAN I october 1956
1954 1957 FORTRAN november 1954 FORTRAN I october 1956 FORTRAN II 1957 FORTRAN III end-1958 B-O 1957 Flow-Matic 1958 COBOL 1959 JOVIAL 1959 IAL 1958 ALGOL 58 1958 Lisp 1958 Lisp 1 1959 Languages september
1 Operational Semantics for While
Models of Computation, 2010 1 1 Operational Semantics for While The language While of simple while programs has a grammar consisting of three syntactic categories: numeric expressions, which represent
COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing
COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing The scanner (or lexical analyzer) of a compiler processes the source program, recognizing
Type Classes with Functional Dependencies
Appears in Proceedings of the 9th European Symposium on Programming, ESOP 2000, Berlin, Germany, March 2000, Springer-Verlag LNCS 1782. Type Classes with Functional Dependencies Mark P. Jones Department
Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions
COMP209 Object Oriented Programming Designing Classes 2 Mark Hall Programming by Contract (adapted from slides by Mark Utting) Preconditions Postconditions Class invariants Programming by Contract An agreement
KITES TECHNOLOGY COURSE MODULE (C, C++, DS)
KITES TECHNOLOGY 360 Degree Solution www.kitestechnology.com/academy.php [email protected] [email protected] Contact: - 8961334776 9433759247 9830639522.NET JAVA WEB DESIGN PHP SQL, PL/SQL
Object-Oriented Software Specification in Programming Language Design and Implementation
Object-Oriented Software Specification in Programming Language Design and Implementation Barrett R. Bryant and Viswanathan Vaidyanathan Department of Computer and Information Sciences University of Alabama
n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation
Lecture Outline Programming Languages CSCI-4430 & CSCI-6430, Spring 2016 www.cs.rpi.edu/~milanova/csci4430/ Ana Milanova Lally Hall 314, 518 276-6887 [email protected] Office hours: Wednesdays Noon-2pm
1/20/2016 INTRODUCTION
INTRODUCTION 1 Programming languages have common concepts that are seen in all languages This course will discuss and illustrate these common concepts: Syntax Names Types Semantics Memory Management We
Implementing Programming Languages. Aarne Ranta
Implementing Programming Languages Aarne Ranta February 6, 2012 2 Contents 1 What is a programming language implementation 11 1.1 From language to binary..................... 11 1.2 Levels of languages........................
ATSBA: Advanced Technologies Supporting Business Areas. Programming with Java. 1 Overview and Introduction
ATSBA: Advanced Technologies Supporting Business Areas Programming with Java 1 Overview and Introduction 1 1 Overview and Introduction 1 Overview and Introduction 1.1 Programming and Programming Languages
TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT
TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT Selvakumar Samuel Asia Pacific University of Technology and Innovation Technology Park Malaysia 57000 Bukit Jalil, Malaysia. Email:
Parameter passing in LISP
Parameter passing in LISP The actual parameters in a function call are always expressions, represented as lists structures. LISP provides two main methods of parameter passing: Pass/Call-by-value. The
Software Paradigms (Lesson 1) Introduction & Procedural Programming Paradigm
Software Paradigms (Lesson 1) Introduction & Procedural Programming Paradigm Table of Contents 1 Introduction... 2 1.1 Programming Paradigm... 2 1.2 Software Design Paradigm... 3 1.2.1 Design Patterns...
The Cool Reference Manual
The Cool Reference Manual Contents 1 Introduction 3 2 Getting Started 3 3 Classes 4 3.1 Features.............................................. 4 3.2 Inheritance............................................
Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C
Parameter Passing In this set of notes you will learn about: Parameter passing modes Call by Call by reference Call by sharing Call by result Call by /result Call by name Subroutine closures as parameters
Parsing Technology and its role in Legacy Modernization. A Metaware White Paper
Parsing Technology and its role in Legacy Modernization A Metaware White Paper 1 INTRODUCTION In the two last decades there has been an explosion of interest in software tools that can automate key tasks
Chapter 5 Names, Bindings, Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Scope Scope and Lifetime Referencing Environments Named
On Understanding Types, Data Abstraction, and Polymorphism
1 Computing Surveys, Vol 17 n. 4, pp 471-522, December 1985 On Understanding Types, Data Abstraction, and Polymorphism Luca Cardelli AT&T Bell Laboratories, Murray Hill, NJ 07974 (current address: DEC
Ed. v1.0 PROGRAMMING LANGUAGES WORKING PAPER DRAFT PROGRAMMING LANGUAGES. Ed. v1.0
i PROGRAMMING LANGUAGES ii Copyright 2011 Juhász István iii COLLABORATORS TITLE : PROGRAMMING LANGUAGES ACTION NAME DATE SIGNATURE WRITTEN BY István Juhász 2012. március 26. Reviewed by Ágnes Korotij 2012.
Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: [email protected] Alfonso Ortega: alfonso.ortega@uam.
Compilers Spring term Mick O Donnell: [email protected] Alfonso Ortega: [email protected] Lecture 1 to Compilers 1 Topic 1: What is a Compiler? 3 What is a Compiler? A compiler is a computer
CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013
Oct 4, 2013, p 1 Name: CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 1. (max 18) 4. (max 16) 2. (max 12) 5. (max 12) 3. (max 24) 6. (max 18) Total: (max 100)
CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014
CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages Nicki Dell Spring 2014 What is a Programming Language? A set of symbols and associated tools that translate (if necessary) collections
The C Programming Language course syllabus associate level
TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming
Introduction to Python
1 Daniel Lucio March 2016 Creator of Python https://en.wikipedia.org/wiki/guido_van_rossum 2 Python Timeline Implementation Started v1.0 v1.6 v2.1 v2.3 v2.5 v3.0 v3.1 v3.2 v3.4 1980 1991 1997 2004 2010
Programming Language Concepts for Software Developers
Programming Language Concepts for Software Developers Peter Sestoft IT University of Copenhagen, Denmark [email protected] Abstract This note describes and motivates our current plans for an undergraduate
Sources: On the Web: Slides will be available on:
C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,
The previous chapter provided a definition of the semantics of a programming
Chapter 7 TRANSLATIONAL SEMANTICS The previous chapter provided a definition of the semantics of a programming language in terms of the programming language itself. The primary example was based on a Lisp
Structure of Presentation. The Role of Programming in Informatics Curricula. Concepts of Informatics 2. Concepts of Informatics 1
The Role of Programming in Informatics Curricula A. J. Cowling Department of Computer Science University of Sheffield Structure of Presentation Introduction The problem, and the key concepts. Dimensions
The Needle Programming Language
The Needle Programming Language The Needle Programming Language 1 What is Needle? Needle is an object-oriented functional programming language with a multimethod-based OO system, and a static type system
Accentuate the Negative: Homework Examples from ACE
Accentuate the Negative: Homework Examples from ACE Investigation 1: Extending the Number System, ACE #6, 7, 12-15, 47, 49-52 Investigation 2: Adding and Subtracting Rational Numbers, ACE 18-22, 38(a),
Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.
Organization of Programming Languages CS320/520N Razvan C. Bunescu School of Electrical Engineering and Computer Science [email protected] Names, Bindings, and Scopes A name is a symbolic identifier used
PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?
1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members
Fundamentals of Programming Languages
A puzzle and a dilemma Suppose I want to capture/catch/slay crashing programs before they have a chance to run (=static type safety). Do I let this one go? let c = ref (fun x -> x) in c := (fun x -> x
Computer Programming I & II*
Computer Programming I & II* Career Cluster Information Technology Course Code 10152 Prerequisite(s) Computer Applications, Introduction to Information Technology Careers (recommended), Computer Hardware
On Understanding Types, Data Abstraction, and Polymorphism
On Understanding Types, Data Abstraction, and Polymorphism LUCA CARDELLI AT&T Bell Laboratories, Murray Hill, N. J. 07974 PETER WEGNER Department of Computer Science, Brown University, Providence, R. I.
ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science
ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science Program Schedule CTech Computer Science Credits CS101 Computer Science I 3 MATH100 Foundations of Mathematics and
THE ROLE OF PROGRAMMING PARADIGMS IN THE FIRST PROGRAMMING COURSES. 1. Introduction
THE TEACHING OF MATHEMATICS 2008, Vol. XI, 2, pp. 63 83 THE ROLE OF PROGRAMMING PARADIGMS IN THE FIRST PROGRAMMING COURSES Milena Vujošević-Janičić and Dušan Tošić Abstract. The choice of the first programming
The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1
The Java Series Java Essentials I What is Java? Basic Language Constructs Slide 1 What is Java? A general purpose Object Oriented programming language. Created by Sun Microsystems. It s a general purpose
Computer Programming I
Computer Programming I COP 2210 Syllabus Spring Semester 2012 Instructor: Greg Shaw Office: ECS 313 (Engineering and Computer Science Bldg) Office Hours: Tuesday: 2:50 4:50, 7:45 8:30 Thursday: 2:50 4:50,
Programming Languages
CS 345 Programming Languages Vitaly Shmatikov http://www.cs.utexas.edu/~shmat/courses/cs345/ slide 1 Course Personnel Instructor: Vitaly Shmatikov Office: CSA 1.114 Office hours: Tuesday, 3:30-4:30pm (after
CS 241 Data Organization Coding Standards
CS 241 Data Organization Coding Standards Brooke Chenoweth University of New Mexico Spring 2016 CS-241 Coding Standards All projects and labs must follow the great and hallowed CS-241 coding standards.
Chapter 3. Input and output. 3.1 The System class
Chapter 3 Input and output The programs we ve looked at so far just display messages, which doesn t involve a lot of real computation. This chapter will show you how to read input from the keyboard, use
Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game
Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game Directions: In mobile Applications the Control Model View model works to divide the work within an application.
Software Development (CS2500)
(CS2500) Lecture 15: JavaDoc and November 6, 2009 Outline Today we study: The documentation mechanism. Some important Java coding conventions. From now on you should use and make your code comply to the
C Compiler Targeting the Java Virtual Machine
C Compiler Targeting the Java Virtual Machine Jack Pien Senior Honors Thesis (Advisor: Javed A. Aslam) Dartmouth College Computer Science Technical Report PCS-TR98-334 May 30, 1998 Abstract One of the
1 Abstract Data Types Information Hiding
1 1 Abstract Data Types Information Hiding 1.1 Data Types Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content
