[erlang-questions] specs for fixed length lists

Gianfranco Alongi <>
Sat Jul 7 15:08:43 CEST 2012


You can (of course with an overhead penalty), do tuple_to_list and use
map / foreach.
But yes, it would be nice if you could somehow define your tuples as
an instance of a Haskell functor.

/G

On Sat, Jul 7, 2012 at 3:03 PM, Daniel Goertzen
<> wrote:
> But tuples lack things like lists:map() and lists:foreach().  If there were
> such functions for tuples, I would agree with you and just use tuples.
>
> Dan.
>
> On Sat, Jul 7, 2012 at 5:15 AM, Joe Armstrong <> wrote:
>>
>> I don't understand.
>>
>> Lists are intended as containers for a variable number of terms. Tuples
>> are
>> intended as a container for a fixed number of terms. Not using lists and
>> tuples
>> as they are intended will just lead to confusion.
>>
>> /Joe
>>
>> On Fri, Jul 6, 2012 at 6:28 PM, Daniel Goertzen
>> <> wrote:
>> > While working with lists of fixed length, I noted that type
>> > specification
>> > for such lists are conspicuously missing.  For example, the following
>> > does
>> > not work...
>> >
>> > -spec get_buttons() -> {ok, [boolean(), boolean(), boolean(),
>> > boolean()]}.
>> >
>> > Is there some sneaky way to make this go?  Now I know you would normally
>> > use
>> > a tuple in this situation, but I will be using the lists module to
>> > process
>> > this data so I want to stick with lists.
>> >
>> > I would love to see something like this for list specification...
>> >
>> >
>> >   List :: list(Type)                        %% Proper list
>> > ([]-terminated)
>> > +       | list(Type, Length)                %% Proper list of fixed size
>> > +       | tlist(TList)                      %% Proper list specified
>> > like a
>> > tuple
>> >         | improper_list(Type1, Type2)       %% Type1=contents,
>> > Type2=termination
>> >         | maybe_improper_list(Type1, Type2) %% Type1 and Type2 as above
>> >
>> >   Tuple :: tuple()                          %% stands for a tuple of any
>> > size
>> >         | {}
>> >         | {TList}
>> >
>> >   TList :: Type
>> >         | Type, TList
>> >
>> >
>> > Thanks,
>> > Dan.
>> >
>> >
>> > _______________________________________________
>> > erlang-questions mailing list
>> > 
>> > http://erlang.org/mailman/listinfo/erlang-questions
>> >
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list