Just getting acquainted with this wonderful system, I’ve run into the following issue:
The shell seems to accept a structure like this: [a | b].
is_list([a | b]) returns true but length([a | b]) raises an exception.
Is it like an “open list” found in some programming languages?
Any help would be appreciated,
LKaroly
What might be confusing here is that 'is_list()' is actually a slight
misnomer, [_ | _] is really just a pair (aka "cons cell") regardless
of what values are held as head + tail. The length() function expects
a "proper list", which is [] or a pair such that its second component
is a proper list.
I'm not familiar with the term "open list", but hope the above helps.
-- Jachym
/Joe Armstrong
2009/2/12 Ladvánszky Károly <l...@digicart.hu>:
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
is_list/1 only inspects the top-most node of the structure and tests
if it's a "list cell" or not. It's poorly named and corresponds to
testing for a "cons" in Lisp dialects.
> Is it like an “open list” found in some programming languages?
Wouldn't know. Never heard of that one.
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
--
View this message in context: http://www.nabble.com/Irregular-list-tp21977982p21979837.html
Sent from the Erlang Questions mailing list archive at Nabble.com.
To test for proper list:
is_proper_list([_|T]) -> is_proper_list(T);
is_proper_list([]) -> true;
is_proper_list(_) -> false.
or from shell:
5> F = fun([_|T],F) -> F(T,F); ([],_) -> true; (_,_) -> false end.
#Fun<erl_eval.12.113037538>
6>
7> F([a|b],F).
false
8> F([a,b],F).
true
9> F([],F).
true
10> F([a,b|c],F).
false
11> F([a,b|[]],F).
true
Zvi
--
View this message in context: http://www.nabble.com/Irregular-list-tp21977982p21980109.html
Thanks everybody for the help.
Sorry for the confusing term ‘open list’; I wished to refer to Lisp’s/Scheme’s unterminated lists.
LKaroly
Mihai
FWIW, the dict module uses them...
--
paul
A 2-tuple is larger than a cons-cell by one word (or 50% if you
prefer :-), so choosing cons over 2-tuple makes practical sense
if you're about to work with a huge amount of pairs. The dict
module from stdlib seems to be an example.
Note this argument isn't inherent to the problem: With some amount
of work, you could probably tweak erts to represent 2-tuples without
the extra overhead.
Other than size, someone has recently proposed a new function for
a stdlib module (sorry, I forgot the details) that used improper
lists, and after reading the explanation, it felt very intuitive
(more so than the 2-tuple based analogy). So forbidding improper
lists altogether might perhaps be something to regret later on.
Just my two cents, anyway.
-- Jachym
Maybe I'm missing something, but what is the point of even allowing
the concept of cons cells? Wouldn't a tuple be more generic?
Is there any practical use of "improper lists" in Erlang?
There are a few of implementation reasons:
First, a cons cell requires 2 words, while a 2-tuple needs 3 words. That's a 50% overhead, not to mention some extra checking.
Second, requiring that lists be proper means you have to check the list cdr every time you cons something. (At least conceptually; a compiler can probably remove some checks where listness can be derived.)
Third, what about using longer tuples to actually save memory? Here, garbage collection becomes an issue. For a list, it's easy to see whether a cons cell is unreachable. For a tuple, it's usually difficult to see that an element is dead and can be collected. Hence you can easily get space leaks. (Though it's not impossible to handle; e.g., lispmachines used cdr-coding, which is the moral equivalent.)
Best,
Thomas