Erlang Reading Assignment

As a prerequisite, you should first read the getting started quickly reference. Then, please read Getting Started with Erlang to learn the basics of the language. In most cases, you can just read it (either a PDF form or online), but for more advanced examples, it will help to copy the programs (cut and paste) into a file and try compiling and executing the example. You can go back to the erlang page to find other pointers and references. And the online course is helpful as well.

Regarding the Getting Started with Erlang document, what follows are some of the main things I noted while reading this.

Defining Functions

Tutorial example:

Notice

  1. use of semicolon to continue definition (the semicolon separates different ca

ses)

  1. the period -- terminating the definition
  2. capital N (variables begin with a capital letter in Erlang)

Another tutorial example:

Note: lowercase is an "atom" (look up in Erlang manual), here used for pattern matching -- essentially this is a polymorphic function.

Here's another example with an atom:

That's a function, called foo, which returns the atom "hi_from_foo". Later on, you may find out about the built-in functions that convert from atoms to lists, and so on.

Function application uses ( ) unlike Haskell, Scheme, Lisp, etc --

As in Haskell, a string of characters is just a sequence, but unlike Haskell, a string is a sequence of the numbers that represent ASCII characters:

In case you're curious, the "18>" is just showing how the shell for Erlang numbers each prompt sequentially.

Pattern Matching like Prolog: the following illustrates

  1. the "|" operator can be used in pattern-matching to split a sequence into head and tail
  2. multiple assignment is possible using lists, tuples, or combinations of lists and tuples
     25> [A, B | C] = [1, 2].
     [1,2]
     26> A.
     1
     27> B.
     2
     28> C.
     []
     29> {X, Y} = {paris, {f, 28}}.
     {paris,{f,28}}
     30> X.
     paris
     31> Y.
     {f,28}

when, if, case --use the "when" clause when simple pattern matching doesn't do what you want:

You can use "if" in defining a function:

Notes:

  1. the "if" statement terminates with an "end"
  2. this examples also shows that you can have multiple function calls in a clause, separated by commas. the final function call (or atom) is the result to be returned

There's also a "case" construct for function definitions:

Notes:

  1. like "if", the "case" terminates with "end"
  2. pattern-matching is used for whatever you specify for cases

You can mix-and-match if, when, case, with comma and semicolon features to build a complex function:

Note: the "Leap" variable was defined for convenience using the if-end construct.

Concurrency

To learn about concurrency, it's a good idea to actually run a few of the tutorials. You can change them and experiment, to learn how things execute. For example, you should make a tut15.erl file, copying the following into the file:

After you've got the file in your own directory, try the erl command, and at the prompt try the following:

The first line compiles tut15.erl, and the second one starts the program.

erlang reading assignment (last edited 2014-05-25 18:23:59 by localhost)