[erlang-questions] Additions to lists module
Mazen Harake
mazen.harake@REDACTED
Thu Nov 27 09:32:32 CET 2008
You are somewhat right but you have to differ between two concepts.
Given a list of elements of arbitrary terms (I will assume we are not
talking about strings now since they happen to be lists but should
perhaps be seen differently) then you should never assume that the order
of these elements are defined. Your functions operating on the list
shouldn't/can't _depend_ on the order...
A good example of this is an optionlist. It would be very naive to
assume that a specific option comes first, and an other specific option
after that one. The order *does* matter, but not in terms of your use of
the list. E.g. if you would perform a set of actions based on atoms in a
list and where one action affect the other... then the order of course
matters... but it shouldn't matter in which order they are, you still
have to perform the operations on the list in the same way. The reason
to reverse a list is to effectivly build a list (this is the most used
case probably) and then get back to the order that it was intended to
have, but when I start using this reversed list I shouldn't care about
the actual order in it (samw concept applies to e.g. sorted lists).
Therefore, the position function proposed is probably not that useful...
but maybe I'm the only one who sees it that way :D
/M
Dave Smith wrote:
>
> 2008/11/26 Mazen Harake <mazen.harake@REDACTED
> <mailto:mazen.harake@REDACTED>>
>
> What is the idea behind pos?
>
> Curious because I have never been in a situation where I need to know
> the actual position of an element since I always assume that the order
> in a list is always undefined. Perhaps I missed something... do
> you have
> a practical example?
>
> /M
>
>
> I'm not following. Lists are ordered and in many cases order is
> meaningful. Otherwise you would never have reason to reverse a list.
>
> There are many functions in the lists module where the result is
> dependant on the lists order, and there is precedence for functions
> like foldlwhile; examples (takewhile/2, dropwhile/2, splitwith/2)
>
More information about the erlang-questions
mailing list