Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Erlang (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Fibonacci sequence=== A tail recursive algorithm that produces the [[Fibonacci sequence]]: <syntaxhighlight lang="erlang"> %% The module declaration must match the file name "series.erl" -module(series). %% The export statement contains a list of all those functions that form %% the module's public API. In this case, this module exposes a single %% function called fib that takes 1 argument (I.E. has an arity of 1) %% The general syntax for -export is a list containing the name and %% arity of each public function -export([fib/1]). %% --------------------------------------------------------------------- %% Public API %% --------------------------------------------------------------------- %% Handle cases in which fib/1 receives specific values %% The order in which these function signatures are declared is a vital %% part of this module's functionality %% If fib/1 receives a negative number, then return the atom err_neg_val %% Normally, such defensive coding is discouraged due to Erlang's 'Let %% it Crash' philosophy, but here the result would be an infinite loop. fib(N) when N < 0 -> err_neg_val; %% If fib/1 is passed precisely the integer 0, then return 0 fib(0) -> 0; %% For all other values, call the private function fib_int/3 to perform %% the calculation fib(N) -> fib_int(N-1, 0, 1). %% --------------------------------------------------------------------- %% Private API %% --------------------------------------------------------------------- %% If fib_int/3 receives 0 as its first argument, then we're done, so %% return the value in argument B. The second argument is denoted _ to %% disregard its value. fib_int(0, _, B) -> B; %% For all other argument combinations, recursively call fib_int/3 %% where each call does the following: %% - decrement counter N %% - pass the third argument as the new second argument %% - pass the sum of the second and third arguments as the new %% third argument fib_int(N, A, B) -> fib_int(N-1, B, A+B). </syntaxhighlight> Omitting the comments gives a much shorter program. <syntaxhighlight lang="erlang"> -module(series). -export([fib/1]). fib(N) when N < 0 -> err_neg_val; fib(0) -> 0; fib(N) -> fib_int(N-1, 0, 1). fib_int(0, _, B) -> B; fib_int(N, A, B) -> fib_int(N-1, B, A+B). </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)