#!/usr/bin/env escript %% -*- erlang -*- %%! -smp enable -sname fibonacci debug verbose %% I've written this as an escript rather than something that we need to compile %% This means it will compile on the fly and execute the function named main main([]) -> %% Getting the input here is simple a single call to an io function Num = io:get_line("\nHow many numbers of the sequence would you like?\n"), %% Erlang does have types - we need to remove the newline and convert the %% input to an integer here before passing it to the fibonacci function fibonacci(0, 1, list_to_integer(string:strip(Num, both, $\n)), 0); main([Num]) -> fibonacci(0, 1, list_to_integer(string:strip(Num, both, $\n)), 0). %% Here we see the elegance of erlang's tail recursion and multiple function %% definitions. Rather than changing variables (which we can't do anyway - %% they are immutable!), we call the function again with new values. When %% the end is reached ( Num == Count ), we exit with ok. Notice that the end %% clause is first so it is exectued when the 3rd and 4th arguments match. fibonacci( _, _, Count, Count ) -> ok; fibonacci( A, B, Num, Count ) -> io:format("~w~n", [A]), fibonacci( B, A + B, Num, Count + 1).