Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

erlang lists:dropwhile weird result

can someone please help me understand what's going on here

lists:dropwhile(fun(X) -> X < 8 end, lists:seq(1,10)).

"\b\t\n" % ??? what is this ? why not [8,9,10]

lists:dropwhile(fun(X) -> X < 7 end, lists:seq(1,10)).  

[7,8,9,10] % this is correct
like image 314
Daya Sharma Avatar asked Apr 06 '12 11:04

Daya Sharma


People also ask

How do I find the length of a list in Erlang?

You can use length() to find the length of a list, and can use list comprehensions to filter your list. num(L) -> length([X || X <- L, X < 1]).

How do I add a list in Erlang?

you will create a new list which is copy of the elements in List1, followed by List2. Looking at how lists:append/1 or ++ would be implemented in plain Erlang, it can be seen clearly that the first list is copied: append([H|T], Tail) -> [H|append(T, Tail)]; append([], Tail) -> Tail.


1 Answers

Your results are actually correct in both cases. The unexpected string in the first case is due to the fact that in Erlang strings are just lists of integers. Therefore, Erlang chooses to interpret your first list as a string, since it contains only printable ASCII codes. In the second case the list contains the code 7, which is not printable, so Erlang is forced to interpret it as an integer list.

You can always print the actual integer list by using

MyList = lists:dropwhile(fun(X) -> X < 8 end, lists:seq(1,10)),
io:format("~w", [MyList]).
like image 194
3lectrologos Avatar answered Sep 23 '22 05:09

3lectrologos