KONADO
KONADO

Reputation: 189

Stack overflow with recursive function

I'm trying to make a recursive function where i pass in a integer and a list. I want to append a certain number of "-" (dashes) to the list if the length of the list is less than the integer as follows:

let rec dashes (longest, l1) =  
    if length l1 = longest then l1 
    else ["-"]@l1@dashes(longest,l1);;

However I get a stack overflow and I'm not sure why.

Upvotes: 0

Views: 58

Answers (1)

Florian Weimer
Florian Weimer

Reputation: 33694

Your recursive call to dashes passes through the original l1 argument, so the list length never grows, and the terminating condition remains false.

Upvotes: 2

Related Questions