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:
“Well designed, fully functional infant. Provides someone to live for as well as another mouth to feed. Produces cooing, gurgling and other adorable sounds. May cause similar behavior in nearby adults. Cries when hungry, sleepy or just because. Hand Wash with warm water and mild soap, then pat dry with soft cloth and talc. Internal mechanisms are self-cleaning... Two Genders: Male. Female. Five Colors: White. Black. Yellow. Red. Camouflage.”
—Alfred Gingold, U.S. humorist. Items From Our Catalogue, Baby, Avon Books (1982)
“If there is a price to pay for the privilege of spending the early years of child rearing in the drivers 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)
“It is hardly to be believed how spiritual reflections when mixed with a little physics can hold peoples attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.”
—G.C. (Georg Christoph)