Skip to content

Instantly share code, notes, and snippets.

@yrashk
Created January 28, 2012 04:26
Show Gist options
  • Save yrashk/aafded15a61bc081fcd1 to your computer and use it in GitHub Desktop.
Save yrashk/aafded15a61bc081fcd1 to your computer and use it in GitHub Desktop.
fib_list(0) -> 0;
fib_list(1) -> 1;
fib_list(N) ->
fib_list(N + 1, [1,0]).
fib_list(End, [H|_]=L) when length(L) == End -> H;
fib_list(End, [A,B|_]=L) ->
fib_list(End, [A+B|L]).
@richcarl
Copy link

Ouch! Don't use length(L) in a loop! It has to traverse the list L to count the elements each time. You could use a separate accumulator variable N to keep track of the length of L.

@yrashk
Copy link
Author

yrashk commented Jan 30, 2012

I agree

@yrashk
Copy link
Author

yrashk commented Jan 30, 2012

Either way, the point was to show one person how lists are constructed

@richcarl
Copy link

Right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment