Created
February 26, 2017 13:01
-
-
Save morgaine/2d10bfdf173de95093735e56ead9ecdf to your computer and use it in GitHub Desktop.
Erlang: commandline interface to factorial using escript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/env escript | |
%% Simplest use of Erlang's escript and commandline arguments | |
%% for an interface to the tail-recursive factorial function. | |
-module(fact). | |
fact(N) -> | |
fact(N, 1). | |
fact(N,Product) when N =< 1 -> | |
Product; | |
fact(N,Product) -> | |
fact(N-1, Product*N). | |
main([]) -> | |
io:format("Usage: fact {integer}~n"), | |
halt(1); | |
main([NumericString|_]) -> | |
N = list_to_integer(NumericString), | |
io:format("fact(~p) = ~p~n", [N, fact(N)]). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for sharing, very useful to see it applied like this!