Erlang (programming Language) - Functional Programming Examples

Functional Programming Examples

A factorial algorithm implemented in Erlang:

-module(fact). % This is the file 'fact.erl', the module and the filename must match -export. % This exports the function 'fac' of arity 1 (1 parameter, no type, no name) fac(0) -> 1; % If 0, then return 1, otherwise (note the semicolon ; meaning 'else') fac(N) when N > 0, is_integer(N) -> N * fac(N-1). % Recursively determine, then return the result % (note the period . meaning 'endif' or 'function end')

A sorting algorithm (similar to quicksort):

%% qsort:qsort(List) %% Sort a list of items -module(qsort). % This is the file 'qsort.erl' -export. % A function 'qsort' with 1 parameter is exported (no type, no name) qsort -> ; % If the list is empty, return an empty list (nothing to sort) qsort -> % Compose recursively a list with 'Front' for all elements that should be before 'Pivot' % then 'Pivot' then 'Back' for all elements that should be after 'Pivot' qsort ++ ++ qsort.

The above example recursively invokes the function qsort until nothing remains to be sorted. The expression is a list comprehension, meaning “Construct a list of elements Front such that Front is a member of Rest, and Front is less than Pivot.” ++ is the list concatenation operator.

A comparison function can be used for more complicated structures for the sake of readability.

The following code would sort lists according to length:

% This is file 'listsort.erl' (the compiler is made this way) -module(listsort). % Export 'by_length' with 1 parameter (don't care of the type and name) -export. by_length(Lists) -> % Use 'qsort/2' and provides an anonymous function as a parameter qsort(Lists, fun(A,B) -> A < B end). qsort(, _)-> ; % If list is empty, return an empty list (ignore the second parameter) qsort(, Smaller) -> % Partition list with 'Smaller' elements in front of 'Pivot' and not-'Smaller' elements % after 'Pivot' and sort the sublists. qsort(, Smaller) ++ ++ qsort(, Smaller).

Here again, a Pivot is taken from the first parameter given to qsort and the rest of Lists is named Rest. Note that the expression

is no different in form from

(in the previous example) except for the use of a comparison function in the last part, saying “Construct a list of elements X such that X is a member of Rest, and Smaller is true", with Smaller being defined earlier as

fun(A,B) -> A < B end

Note also that the anonymous function is named Smaller in the parameter list of the second definition of qsort so that it can be referenced by that name within that function. It is not named in the first definition of qsort, which deals with the base case of an empty list and thus has no need of this function, let alone a name for it.

Read more about this topic:  Erlang (programming Language)

Famous quotes containing the words functional, programming and/or examples:

    In short, the building becomes a theatrical demonstration of its functional ideal. In this romanticism, High-Tech architecture is, of course, no different in spirit—if totally different in form—from all the romantic architecture of the past.
    Dan Cruickshank (b. 1949)

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)