Tail recursion in Erlang?

Your implementation is not tail recursive because append must hold onto the head of the list while computing the tail. In order for a function to be tail-recursive the return value must not rely on an value other than the what is returned from the function call you could rewrite it like so: append(Acc, ) -> %% termination; Acc; append(Acc, H|T) -> Acc2 = Acc ++ dosomethingto(H); %% maybe you meant this to be your write function? Append(Acc2, T); %% tail rercursive Notice that all the work is finished once the tail recursive call occurs.So the append function can forget everthing in the function body and only needs to remember the values of the arguments it passes into the next call Also notice that I put the termination clause before the recursive clause.

Erlang evaluates the clauses in order and since termination clauses are typically more specific the less specific recursive clauses will hide them thus preventing the function from ever returning, which is most likey not your desired behaviour.

Your implementation is not tail recursive because append must hold onto the head of the list while computing the tail. In order for a function to be tail-recursive the return value must not rely on an value other than the what is returned from the function call. You could rewrite it like so: append(Acc, ) -> %% termination; Acc; append(Acc, H|T) -> Acc2 = Acc ++ dosomethingto(H); %% maybe you meant this to be your write function?

Append(Acc2, T); %% tail rercursive Notice that all the work is finished once the tail recursive call occurs. So the append function can forget everthing in the function body and only needs to remember the values of the arguments it passes into the next call. Also notice that I put the termination clause before the recursive clause.

Erlang evaluates the clauses in order and since termination clauses are typically more specific the less specific recursive clauses will hide them thus preventing the function from ever returning, which is most likey not your desired behaviour.

1 for the explanation. You have a typo in the first append. It should return Acc instead of Acc2 – filippo Apr 17 '10 at 20:57 whoops your right.

Fixed now. – Jeremy Wall Apr 18 '10 at 3:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions