tags:

views:

106

answers:

2

Is it possible to write a faster equivalent to this function?

prepend(X, Tuple) ->
  list_to_tuple([X | tuple_to_list(Tuple)]).
+4  A: 

It looks to me like that sort of thing is discouraged. If you want a list, use one.

Getting Started with Erlang:

Tuples have a fixed number of things in them.
Nathon
Definitely, if you want a dynamic structure use lists instead.
rvirding
My actual use case is that I have several functions which return "raw" tuples (e.g. `{Name, Age}`) and would prefer to turn them into tagged tuples (`{person, Name, Age}`). I don't want a dynamic structure at all.
Alexey Romanov
is deeper structure ok for you? like {person, {Name, Age}}? So you could call function with this tag: {person, who(Args)}..
With deeper structure, I'd get _some_ of what I want, but not all.
Alexey Romanov
+2  A: 

If you have a finite number of possible tuple lengths, you could do this:

prepend(X, {}) -> {X};
prepend(X, {A}) -> {X, A};
prepend(X, {A, B}) -> {X, A, B};
prepend(X, {A, B, C}) -> {X, A, B, C}.

You can continue this pattern for as long as you need.

Greg Hewgill