This proposal follows exactly the sections in http://www.oreilly.com/oreilly/author/ch02.html Detailed Outline Index1. An Overview Of Erlang1.1. Your first ever compiled Erlang program 1.2. Writing your first Erlang script 1.3. Factorial 1.4. Pattern matching 2. Sequential programming 2.1. primitive data types 2.2. lists and tuples 2.3. lists (the library) 2.4. map, filter, split 2.5. list comprehensions 2.6. bignums 2.7. functions and clauses 2.8. modules, imports and exports 2.9. funs 2.10. records 2.11. catch/throw 2.12. macros 3. Sequential libraries and patterns 3.1. lists 3.2. file 3.3. code 3.4. map/filter/splitwith 3.5. regexp 3.6. ets 3.7. dets 4. Real sequential applications 4.1. Yecc/Leex 4.2. find 4.3. Rsync 4.4. remove_dups 4.5. rsa 4.6. MD5 authentication 5. Interlude - errors 5.1. Theory of error handling 5.2. How to code 6. Concurrent programming 6.1. processes 6.2. links 6.3. Distribution 6.4. Sockets (gen_tcp) 6.5. epmd 6.6. promises 7. Concurrent libraries and patterns 7.1. gen_servers 7.2. What more? 8. Real Concurrent applications 8.1. The name server 8.2. The client code 8.3. Making the server into a demon process 8.3.1. FreeBSD 8.3.2. Port redirection 8.4. Starting the demon 8.5. socket authentication with MD5 8.6. eol (erlang on line() 8.7. peer-to-peer 8.8. client server 9. Interlude - systems programming 10. The OTP application structure (theory) 10.1. gen server 10.2. supervision 10.3. logger 10.4. FSM 11. A real word OTP application 12. State of the art 12.1. SAE 12.2. egtk 13. Appendix 1 - Obtaining the system 14. Appendix 2 - Installing the system 15. Appendix 3 - Major Libraries functions 16. Appendix 4 - Quick ref to building an OTP application 17. Appendix 5 - Nitty gritty 17.1. Making an Erlang daemon on Linux or free BSD 17.2. Windows demons 17.3. Remote shell debugging 17.4. Windows demons 17.5. Erlang shell editing commands 17.6. Debugger 17.7. Cookies 17.8. Compiling 17.9. rigging epmd 17.10. Tracing 18. Appendix 6 - Erlang description 19. Appendix 7 - Erlang manual page What will the book be good for?Answer http://www.oreilly.com/oreilly/author/ch02.html#whatgoodThis book is targeted at:
The emphasis will be on "getting things done" and we will try very hard to dispell any myths that functional programming is only for mathematicians". We will show programs that are an order of magnitude shorter than Perl or Java and which are easier to read and write. The theme will be "fun code that works" the domain will be distributed applications. We'll do a lot examples with simple peer/peer and client/server architectures. We'll show how to use Erlang and the Erlang libraries to make fun and interesting applications. Things like distributed ICQ/IRC/Napster client/servers, web servers, web caches, wiki webs, peer/peer chat systems and the like. You can run the wiki web to get an idea of what the completed application looks like. The book is intended to be used together with the Open Source Erlang distribution (see www.erlang.org It will make heavy use of the OTP libraries (which are part of the open source distribution). Erlang is a new (ish) concurrent programming language designed for programming soft real-time distributed fault-tolerant applications. Erlang is also the foremost of a new generation of functional programming languages designed for real-world applications. Erlang has been used for a number of heavy-weight and commercially successful applications. The Open-Source Erlang distribution (which was released in 1998) contains an extremely rich set of tools for simplifying the construction of fault-tolerant distributed system. The Market for the BookWe must answer http://www.oreilly.com/oreilly/author/ch02.html#themarket
There is already a very enthustiastic Erlang community - this book will help them and help spread the language. Erlang is spreading rapidly (without a good book, and without any advertising budget) - with a good book it will spead even more rapidly. The market for this book are primarily people interested in learning a functional programming language. Unfortunately functional programming is view with suspicion by many programmers who think that FP is all about Greek and requires a deep knowledge of mathematics Nothing could be further from the truth. The authors have taught Erlang to hundreds of regular programmers (i.e. programmers skilled in C/Java/perl). Once converted these programmers usually love Erlang and would never go back to low level languages like Java or Perl. We believe that declarative programming language offer incredible advantages over conventional languages - our work with Erlang proves this without doubt - we feel it is now time to introduce this to a wider audience. In comp.lang.functional I often read posts from people who are keen to try out a new functional programming language (because they've heard that FP is cool) but who are then frightened off by a heavy dose of theory and a swarm of "monads loving purists" who frighten off newcomers. The functional programming world is split into two schools (the *heavy* theory schools (Monads, lazy polymorphic strong typing etc.) and the "impure" get things done school (scheme, Erlang, ...). The former school of programming is well represented in the "classic literature", but the latter is poorly represented. many people on the Erlang list and on comp.lang.functional have expressed the opinion that "What Erlang needs is a good O'Reilly book" (a la Perl book) - I quite agree (hence this proposal)
Erlang was designed to be "an easy to use functional programming language" it uses dynamic typing (a la smalltalk/scheme) and has a simple process based concurrency model.
Erlang was released as Open Source in 1998. Tens of thousands of downloads of the system have been made and there is a an increasingly enthusiastic user group and mailing list.
One primary obstacle to the widespread use of Erlang is "a good hands-on book". The proposed book is an attempt to solve this problem.
The first every book "Concurrent programming in Erlang" (Prentice hall) described the language - but not the libraries. 1. An Overview Of Erlang
1.1. Your first ever compiled Erlang programTo avoid plagiarism, we start with "Goodbye world":
Use you favorite editor to store this in a file. Call the file goodby.erl. Now start the erlang system, by typing erl, compile the program and run it:
That's it you're up and running. The command erl starts the Erlang shell. c(goodby> compiled the module contained in the file goodbye.erl and goodby:world() evaluated the function world() in the module goodbye. 1.2. Writing your first Erlang scriptIf you want to run "goodby world" as a script, then create file called goodbye.sh containing the following:
This can be run as follows:
Running Erlang programs as shell scripts is not as efficient as compiling programs, but is often a lot more convenient. 1.3. FactorialNow you'll want to progress to something more serious, like factorial -- this is a recursive (Don't panic) function. Just for fun we'll implement this as a shell script:
Running it is easy:
Although this example was very simple it has illustrated a number of points.
1.4. Pattern matching2. Sequential programming
2.1. primitive data typesThere are 4 primitive data types are atoms (examples monday, red), floats (examples 3.14159, 1.23e29 and integers (examples, 1234, 129872894696491649264961249169486). 2.2. lists and tuplesThere are two compound data types. tuples written {T1,T2,..,Tn} and lists written [T1,T2,..,Tn] where T1, T2, .. Tn are either primitive or compound data types. Tuples are like structs in C and are used for storing a fixed number of data items. Lists are like linked lists in C and are used for storing a variable number of data items. 2.3. lists (the library)Examples from the list library. 2.4. map, filter, splitA few simple examples of some of the higher order functions in the lists library. We start off with higher order list programming (paradoxically this is very easy to understand) you can understand this even if you don't know what a list is. We do this though dialogues with the shell. Here's an example dialogue with the shell:
This is easy to explain and understand.
Having given the examples, we can then move readily explain the syntax of lists, what they "mean" etc. Moving on from lists, we introduce tuples and functions over tuples:
2.5. list comprehensionsList comprehensions are wonderful. To get an idea let's try to compute all Pythagorean triangles, having a sides of total length less than N:
We can test this as follows:
The tuple {12,5,13} represents a Pythagorean (right angled) triangle since 12^2 + 5^2 = 13^2 wait a moment I can check this in the Erlang shell
and 12+5+13 = 30 which is less than 35. The notation [X || Y ] means construct the set of X such that Y is true. The symbol || is pronounced such that. P <- Q is read P is taken from Q. In English we can read the program as follows:
Then we'll do list comprehensions, leading up to quick-sort. Again easy:
This is rather nice, quick-sort in two statements. Simple, understandable, efficient. This can be read declaratively like this:
2.6. bignumsNow a little diversion with bignums.
Followed by an explanation. The purpose is twofold. Firstly we get the idea that bignums are more convenient to use and think about than fixnums (think 32 bit) - this fact usually impresses people :-) secondly, and more importantly, that use of bignums eliminates a large class of programming errors (integer overflow). 2.7. functions and clausesnow, that the ground is repaired, we show how to write functions over lists and tuples, we've already used map and sort and reverse etc. So now we show how to implement them as recursion over lists. map is easy:
Or it will be when we have finished with it. 2.8. modules, imports and exports2.9. funs2.10. records2.11. catch/throw2.12. macrosFinally we run through macros and records. At the end of this chapter the reader should have a good idea as to how to write sequential functions. 3. Sequential libraries and patternsWe go through the major libraries. Lots of examples of the most useful and most commonly used function. 3.1. lists3.2. file3.3. code3.4. map/filter/splitwith3.5. regexp3.6. ets3.7. detsThe purpose of this chapter is to illustrate the major libraries, and how they are used.
For example, here are some simple and very commonly used list processing functions:
4. Real sequential applications4.1. Yecc/Leex4.2. find4.3. Rsync4.4. remove_dups4.5. rsa4.6. MD5 authentication5. Interlude - errors5.1. Theory of error handlingThe Erlang way of handling errors is one of the most misunderstood points of the language. The Erlang principle
Don't mess up beautiful code which handles normal cases with messy code for error handling. Suppose deep down in some Erlang program we require a function which converts the atom a to the integer 1, and the atom b to the integer 2. Call this function tr. Obviously we can define tr/1 thus:
So far so good -- now what about errors? - The Erlang way of doing things is to totally separate the code for error handling from the place were the error occurred
In the application program makes a mistake, for example calling tr(c) -- the application process will fail and the error handling process IF WRITTEN will correct the error. Note, you don't always need to write an error handling process. You only need an error handling process if you want to try to correct the error at run-time. If you don't write an error handling process then the process were the error occurred will crash and a message sent to the system "error_logger" process. The error logger reports the error on standard IO (though this behavior can be changed). What do we find in practice? Often people write this type of code:
Oh dear! -- Firstly this code has transformed a CORRECT PROGRAM into an INCORRECT PROGRAM. After all in our original specification tr(c) was undefined -- in the modified program tr(c) HAS a value (i.e. whatever the code in "...do something ..." returns). So what should you do when the error occurs? When you read code that handles exceptional cases like this you will find that the _writer_ of the program doesn't usually know what to do either. One often finds code like this:
Is that any better? - Yes and No. We get a helpful diagnostic BUT tr(c) evaluates to 'ok'. This will most probably cause the program to crash at some other point in time. The original version:
Was actually much better. Since:
Other attempts to fix errors use "catch" and "throw" - now we put the emphasis on error handling on the caller:
Now we have the same problem as before only its been pushed to a different part in the code. What do we write in the "... do something ..." part of the code? Something is seriously wrong here, we can't continue. Write an error diagnostic and crash BUT THIS IS THE DEFAULT BEHAVIOR WITHOUT writing any code at all!
5.2. How to code6. Concurrent programming6.1. processes6.2. links6.3. Distribution6.4. Sockets (gen_tcp)6.5. epmd6.6. promises7. Concurrent libraries and patterns7.1. gen_servers7.2. What more?8. Real Concurrent applications
8.1. The name serverWe develop a name server that will be used for peer-to-peer applications. The name server uses Diffie-Hellman for key exchange, MD5 for authentication, RSA for secure identification and DES for data encryption.8.2. The client codeWe develop a fault-tolerent client.8.3. Making the server into a demon processThis note describes how to make an Erlang daemon. A daemon is a program that runs all the time in the background on your computer. It is started when the machine boots and is stopped when the system is stopped.
I use the two scripts wiki-server.sh which must be placed in /usr/local/etc/rc.d/ and wiki.sh. When the system starts all the scripts in /usr/local/etc/rc.d/
are executed with argument start. Here is wiki-server.sh.
/home/wiki/wiki.sh runs as the user wiki
and contains the following:
Evaluating wiki.sh start starts the wiki daemon. The script is simple. The only points to notice are:
8.4. Starting the demon8.5. socket authentication with MD5We should how various authentication algorithms can be programmed in Erlang.8.6. eol (erlang on line()Associate dynamic IP with fixed address using an FTP server8.7. peer-to-peerWe develop a complete P2P application - P2P backup based on the idea If you provide a back-up of my data then I'll provide a back-up of your data.8.8. client serverWe show how to program a fuaklt-tolerand simple client/server application using the OTP libraries.9. Interlude - systems programmingThis is the "theory" of the OTP stuff. 10. The OTP application structure (theory)OTP is a set of libraries for building fault-tolerant applications. We describe the theory underlying the library structure.This is similar to otp design principles 10.1. gen serverOPT doc - improved.10.2. supervisionOPT doc - improved.10.3. loggerOPT doc - improved.10.4. FSMOPT doc - improved.11. A real word OTP application12. State of the artThis is for stuff that (at the time of going to press hasn't yet made it into the system). These are source forge project or similar. 12.1. SAEMy stand-alone Erlang. How to package an entire application into a very small number of files.12.2. egtkTonys GTK graphics stuff
13. Appendix 1 - Obtaining the system14. Appendix 2 - Installing the system15. Appendix 3 - Major Libraries functions16. Appendix 4 - Quick ref to building an OTP application17. Appendix 5 - Nitty gritty17.1. Making an Erlang daemon on Linux or free BSD17.2. Windows demons17.3. Remote shell debugging17.4. Windows demons17.5. Erlang shell editing commands17.6. Debugger17.7. Cookies17.8. Compiling17.9. rigging epmd17.10. Tracing18. Appendix 6 - Erlang descriptionA much compressed version of the Erlang spec.
19. Appendix 7 - Erlang manual page
Schedulehttp://www.oreilly.com/oreilly/author/ch02.html#scheduleToolsI'd like to write this in EHTML. |