[erlang-questions] Baffling lists

Stu Bailey stu.bailey@REDACTED
Sun Sep 21 17:52:18 CEST 2014


Thanks to another comment, I see this is "cons" at work making lists where
the last element is not a list, as Dmytro said.   I got it now.

130> [[hello] | hello].

[[hello]|hello]

131> [world,[hello] | hello].

[world,[hello]|hello]

132> List5 = [world,[hello] | hello].

[world,[hello]|hello]

133> lists:reverse(List5).

** exception error: bad argument

     in function  lists:reverse/2

        called as lists:reverse(hello,[[hello],world])

     in call from lists:reverse/1 (lists.erl, line 152)

134> List5.

[world,[hello]|hello]

135> is_list(List5).

true

136>

That is a bit quirky, but hey it's Erlang!  The quick hack for my code was
to replace "lists:reverse(L4)" with my little strip_lists_rev(L4).


strip_lists_rev(0,Acc)->
    Acc;
strip_lists_rev([H|Rest],Acc)->
    strip_lists_rev(Rest,[H|Acc]).


Hopefully, this won't impact performance and reliability too much.

Thanks again.

On Sun, Sep 21, 2014 at 8:07 AM, Stu Bailey <stu.bailey@REDACTED> wrote:

> Thanks.  I actually wanted to reverse the list of lists
> lists:reverse(L4).  I did not know about "irregular lists".  Now I do.
>
> Thanks again!
>
>
> On Sun, Sep 21, 2014 at 8:00 AM, Dmytro Lytovchenko <
> dmytro.lytovchenko@REDACTED> wrote:
>
>> Oh and also its list of another list and 0 as tail element. You probably
>> want to reverse first element of L4, like lists:reverse(hd(L4)).
>>
>> On Sun, Sep 21, 2014 at 4:51 PM, Dmytro Lytovchenko <
>> dmytro.lytovchenko@REDACTED> wrote:
>>
>>> It is irregular list, that is list which doesn't have [] as tail
>>> element, and has 0 there instead. While its legal to have such a value,
>>> some list handling functions will be waiting for [] as tail element thus
>>> they will be confused.
>>> Solution: don't have 0 there, or cut it away temporarily and append
>>> later as X = [CutList | 0].
>>>
>>> On Sun, Sep 21, 2014 at 4:46 PM, Stu Bailey <stu.bailey@REDACTED>
>>> wrote:
>>>
>>>> I'm sure I'm missing something obvious, but here goes:
>>>>
>>>> What is going on here?  Is L4 a list?  If so, why can't I reverse it?
>>>>
>>>> 70> L4.
>>>>
>>>> [[<<"8">>,<<"7">>,<<"6">>,<<"5">>],
>>>>
>>>>  [<<"4">>,<<"3">>,<<"2">>,<<"1">>]|
>>>>
>>>>  0]
>>>>
>>>> 71> lists:reverse(L4).
>>>>
>>>> ** exception error: bad argument
>>>>
>>>>      in function  lists:reverse/2
>>>>
>>>>         called as lists:reverse(0,
>>>>
>>>>                                 [[<<"4">>,<<"3">>,<<"2">>,<<"1">>],
>>>>
>>>>                                  [<<"8">>,<<"7">>,<<"6">>,<<"5">>]])
>>>>
>>>>      in call from lists:reverse/1 (lists.erl, line 152)
>>>>
>>>> 72> [Head1|Rest1] = L4.
>>>>
>>>> [[<<"8">>,<<"7">>,<<"6">>,<<"5">>],
>>>>
>>>>  [<<"4">>,<<"3">>,<<"2">>,<<"1">>]|
>>>>
>>>>  0]
>>>>
>>>> 73> Rest1.
>>>>
>>>> [[<<"4">>,<<"3">>,<<"2">>,<<"1">>]|0]
>>>>
>>>> 74> lists:reverse(Rest1).
>>>>
>>>> ** exception error: no function clause matching
>>>> lists:reverse([[<<"4">>,<<"3">>,<<"2">>,<<"1">>]|0]) (lists.erl, line 145)
>>>>
>>>> 75> [Head2|Rest2] = Rest1.
>>>>
>>>> [[<<"4">>,<<"3">>,<<"2">>,<<"1">>]|0]
>>>>
>>>> 76> Head2.
>>>>
>>>> [<<"4">>,<<"3">>,<<"2">>,<<"1">>]
>>>>
>>>> 77> Rest2.
>>>>
>>>> 0
>>>>
>>>> 78>
>>>>
>>>>
>>>> In practice, the kind of structure demonstrated in L4 is being returned
>>>> by a function from another library I'm trying to use, but I'm trying to
>>>> understand what's going on in general.
>>>>
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> erlang-questions@REDACTED
>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140921/1842946c/attachment.htm>


More information about the erlang-questions mailing list