diff options
author | Henrik Nord <[email protected]> | 2014-02-07 11:42:50 +0100 |
---|---|---|
committer | Henrik Nord <[email protected]> | 2014-02-07 11:42:50 +0100 |
commit | 069b1840316979c69e4bfed1c814a239b852d8ba (patch) | |
tree | eaea7e45e5ab265f1f85d7ff63f69e2c482f0009 /lib/stdlib/src | |
parent | 76181f2e146dea3638e70a8636690ab8b4397c70 (diff) | |
parent | 875b58c8119858676af0139b0f6b69537cae5706 (diff) | |
download | otp-069b1840316979c69e4bfed1c814a239b852d8ba.tar.gz otp-069b1840316979c69e4bfed1c814a239b852d8ba.tar.bz2 otp-069b1840316979c69e4bfed1c814a239b852d8ba.zip |
Merge branch 'hsv/add_droplast_to_lists'
* hsv/add_droplast_to_lists:
Added documentation of lists:droplast/1
Added tests for lists:droplast/1 to stdlib/lists_SUITE
stdlib/lists: Add function droplast/1 This functions drops the last element of a non-empty list.
Diffstat (limited to 'lib/stdlib/src')
-rw-r--r-- | lib/stdlib/src/lists.erl | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl index d6a9f4645d..6303465d3d 100644 --- a/lib/stdlib/src/lists.erl +++ b/lib/stdlib/src/lists.erl @@ -22,7 +22,7 @@ -compile({no_auto_import,[min/2]}). -export([append/2, append/1, subtract/2, reverse/1, - nth/2, nthtail/2, prefix/2, suffix/2, last/1, + nth/2, nthtail/2, prefix/2, suffix/2, droplast/1, last/1, seq/2, seq/3, sum/1, duplicate/2, min/1, max/1, sublist/2, sublist/3, delete/2, unzip/1, unzip3/1, zip/2, zip3/3, zipwith/3, zipwith3/4, @@ -203,6 +203,19 @@ suffix(Suffix, List) -> Delta = length(List) - length(Suffix), Delta >= 0 andalso nthtail(Delta, List) =:= Suffix. +%% droplast(List) returns the list dropping its last element + +-spec droplast(List) -> InitList when + List :: [T, ...], + InitList :: [T], + T :: term(). + +%% This is the simple recursive implementation +%% reverse(tl(reverse(L))) is faster on average, +%% but creates more garbage. +droplast([_T]) -> []; +droplast([H|T]) -> [H|droplast(T)]. + %% last(List) returns the last element in a list. -spec last(List) -> Last when |