我的雲端生活網 - Life+

Friday, July 6, 2007

The factorial's tail recursive

Erlang:

-module(test).
-export([fac/1]).
fac(N) -> fac(N,1).

fac(0,A) -> A;
fac(N,A) -> fac(N-1,N*A).
--------------------------------
Haskell:

module Fac
where

fac(n)=facit(n,1)
facit(0,m) = m
facit(n,m)=facit(n-1,n*m)

No comments:

Blog Archive