[erlang-questions] ++ to select the head ( instead of just the tail )
Raimo Niskanen
raimo+erlang-questions@REDACTED
Thu Aug 18 09:48:36 CEST 2011
On Wed, Aug 17, 2011 at 11:27:26PM +0200, Adams Christian wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Maybe dumb .. but why don't you just
>
> RL = lists:reverse(List).
> "654" ++ RTail = RL.
> Head = lists:reverse(RTail).
>
> Just my 2c ..
That is how you do it, but if you have a huge List you will
have to watch out since you build RTail just to become garbage.
A solution that not builds the result in reverse as garbage
would have to be a two-pass solution. First finding if there
is a match and the resulting Head length. Then building the
list forwards with non-tail recursion.
That solution assumes that the stack for non-tail recursion
grows exactly as much as the heap grows when building one
result list element, and that the heap and stack shares
memory block. This is the case for the current BEAM implementation
but not for e.g Erjang.
These issues are most likely why "654"++RTail is allowed since
that is just syntactical sugar for [$6,$4,$5|RTail], really
easy to define and implement, while Head++"456" is not allowed
in a pattern since the implementation is if not non-trivial
certainly inefficient. The only O(N) operation I know of
allowed in a pattern match (guard) is length(List) and it
should not get any partners in misery.
I think the OP asked chiefly for symmetry reasons, and the answer
might be that "654"++RTail and Head++"456" may look symmetrical
but require very different implementations.
>
> CA
>
> Am 16.08.2011 um 13:23 schrieb James Churchman:
>
> > The ++ operator is not just a righthand side operator, but can in fact be a lefthand side operator in as far as I can tell just 1 case :
> >
> > When you know the head you wish to remove and getting the tail :
> >
> > Eg in the shell:
> >
> > FullList = "123456".
> > "123" ++ Tail = FullList.
> > Tail wil now be "456"
> >
> > However using it to get the head is not supported :
> >
> > FullList = "123456".
> > Head ++ "456" = FullList.
> >
> > gives : * 1: illegal pattern
> >
> > Understandably this second type involves traversing the entire list, where the first does not, but it would still be a nice addition to erlang.
> >
> > Was this not added as the first one expands out to simply [$1,$2,$3 | Tail] etc... but the second needs to expand to something more complex like an actual function the way list comprehensions do?
> >
> > James
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
>
> CHRISTIAN ADAMS
> Software Systems Engineer
> Fon: +49 651 99554792
> christian.adams@REDACTED
>
> SOL VENETUS Software GmbH
> Sebastian-Kneipp-Str. 2 - 86482 Aystetten - Germany
> Fon: +49 170 7762480
> Fax: +49 821 4865786
> Geschäftsführer: Benjamin Hilbig
> Sitz der Gesellschaft: Aystetten
> Amtsgericht Augsburg, HRB 24559
> USt-IdNr.: DE267014985
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
>
> iEYEARECAAYFAk5MMj4ACgkQr81gVylJyzFG1ACeI47hG24NJC2/5xRm+ZQw2ura
> AqwAoOL6Ch259AymGgGTVh5p2Q513dxS
> =OQ1H
> -----END PGP SIGNATURE-----
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
--
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list