Functional Programming
ii
c Copyright 1992–1995 Jeroen Fokker and
Department of Computer Science, Utrecht University This text may be reproduced for educational purposes under the following restrictions: • the text will not be edited or truncated; • especially this message will be reproduced too; • the duplicates will not be sold for profits; You can reach the author at: Jeroen Fokker, Vakgroep Informatica, Postbus 80089, 3508 TB Utrecht, e-mail
[email protected]
Dutch edition: 1st print September 1992 2nd print February 1993 3rd reviewed print September 1993 4th reviewed print September 1994 5th reviewed print September 1995 Spanish edition, translated by Hielko Ophoff: based on 4th reviewed print September 1994 English edition, translated by Dennis Gruijs and Arjan van IJzendoorn: based on 5th reviewed print September 1995
iii
Contents 1 Functional Programming 1 1.1 Functional Languages 1 1.1.1 Functions 1 1.1.2 Languages 1 1.2 The Gofer-interpreter 2 1.2.1 Evaluating expressions 2 1.2.2 Defining functions 4 1.2.3 Internal commands 5 1.3 Standard functions 5 1.3.1 Primitive/predefined 5 1.3.2 Names of functions and operators 6 1.3.3 Functions on numbers 7 1.3.4 Boolean functions 8 1.3.5 Functions on lists 8 1.3.6 Functions on functions 9 1.4 Function definitions 9 1.4.1 Definition by combination 9 1.4.2 Definition by case distinction 10 1.4.3 Definition by pattern matching 11 1.4.4 Definition by recursion or induction 12 1.4.5 Layout and comments 13 1.5 Typing 14 1.5.1 Kinds of errors 14 1.5.2 Prototyping of expressions 15 1.5.3 Polymorphism 16 1.5.4 Functions with more parameters 17 1.5.5 Overloading 17 Exercises 18 2 Numbers and functions 21 2.1 Operators 21 2.1.1 Operators as functions and vice versa 21 2.1.2 Priorities 2