0
%%% @doc Additional list functions, inspired by Haskell's Data.List.
0
-%%% @author Hasan Veldstra <hasan@12monkeys.co.uk>
0
+%%% @author Hasan Veldstra <hasan@12monkeys.co.uk>
[http://12monkeys.co.uk]0
%%% Distributed under the same license as Erlang.
0
--import(lists, [all/2, append/1,
foreach/2, reverse/1]).
0
+-import(lists, [all/2, append/1,
duplicate/2, foldl/3, foreach/2, map/2, reverse/1, seq/2]).
0
%% List transformations.
0
--export([intersperse/2, intercalate/2]).
0
+-export([intersperse/2, intercalate/2, transpose/1]).
0
%% @doc Returns all the elements of a list except the last one.
0
-%% The list must be finite and non-empty.
0
-init(L) when is_list(L) andalso L =/= [] ->
0
+init(L) when is_list(L) ->
0
reverse(tl(reverse(L))).
0
%% @doc Takes an element and a list and 'intersperses' that element between
0
%% the elements of the list.
0
-intersperse(Elt, L) when is_list(L)
->
0
+intersperse(Elt, L) when is_list(L)
andalso L =/= [] ->
0
tl(intersperse1(Elt, L, [])).
0
intersperse1(Elt, [Hd | Tl], Acc) ->
0
@@ -30,3 +32,32 @@ intersperse1(_Elt, [], Acc) ->
0
intercalate(Xs, Xss) ->
0
append(intersperse(Xs, Xss)).
0
+%% @doc Transposes the rows and columns of its argument. For example:<br />
0
+%% <code>transpose([[1, 2, 3], [a, b, c]]) = [[1, a], [2, b], [3, c]]</code> <br />
0
+%% All sub-lists must be of the same length.
0
+transpose(L = [Row | _Restrows]) ->
0
+ lists:foreach(fun(X) ->
0
+ Acc = duplicate(Len, []),
0
+transpose1([Row | Restrows], Acc) ->
0
+ Newacc = lists:zipwith(fun(X, Y) ->
0
+ transpose1(Restrows, Newacc);
0
+%% @doc Computes the product of a list of numbers.
Comments
No one has commented yet.