7

I am writing a program that can have either a list or a string as an argument. How can I tell the difference between a string and a list programmatically in Erlang. Something like:

print(List) -> list; 
print(String) -> string.

4 Answers 4

6

io_lib:printable_list might be what you are looking for. However it doesn't handle unicode only latin-1 encodings. If you need to detect unicode strings I think you might be out of luck. The best bet is pseudo typing your lists like so: {string, [$a, $b, $c]}. Kind of a build your types.

Use a constructor like so string(L) when is_list(L) -> {string, L}. and just use that typing construct all through your app.

On the other hand you could just treat all strings as just lists and not make the distinction.

2
  • Thanks. yes, I did think of this pseudo typing, but I didn't know if it was a good way to do things in Erlang.
    – yazz.com
    Mar 19, 2010 at 20:16
  • Another option I've seen used is to keep all strings as binaries. Mar 24, 2010 at 15:41
5

Best thing what you can do is tagging your structures as Jeremy Wall suggested. Anyway you can decide check input to your module/subsystem/application/...

is_string([]) -> true;
is_string([X|T]) -> is_integer(X) andalso X>=0 andalso is_string(T);
is_string(_) -> false.

Unfortunately it is expensive operation and you can't use it in guards.

2
  • But then a list would show up as a string in your example, is that correct?
    – yazz.com
    Mar 19, 2010 at 21:40
  • I don't understand question. Do you mean something like new_string(X) -> true = is_string(X), {string, X}.? So answer is yes. Mar 19, 2010 at 22:50
4

Erlang implements different functions to test if a list is a flat list in module io_lib. Despite Jeremy Wall comment there is a function to test if a flat list contains unicode characters as well as latin1 version.

If you want to test for flat unicode lists you can use io_lib:char_list(Term) http://erlang.org/doc/man/io_lib.html#char_list-1

io_lib:char_list/1 function implementation is:

char_list([C|Cs]) when is_integer(C), C >= 0, C < 16#D800;
       is_integer(C), C > 16#DFFF, C < 16#FFFE;
       is_integer(C), C > 16#FFFF, C =< 16#10FFFF ->
    char_list(Cs);
char_list([]) -> true;
char_list(_) -> false.

One good choice for checking latin1 encoded strings is io_lib:latin1_char_list(Term) http://erlang.org/doc/man/io_lib.html#latin1_char_list-1

io_lib:latin1_char_list/1 function implementation is:

latin1_char_list([C|Cs]) when is_integer(C), C >= $\000, C =< $\377 ->
      latin1_char_list(Cs);
latin1_char_list([]) -> true;
latin1_char_list(_) -> false.

Check the io_lib module documentation for other similar functions.

0
1

Why would you need to separate these? Strings are lists in erlang (most of the time).

2
  • 1
    The most common reason you would need to do this is a nested lists in a tree where some of the sub-lists are strings which need to be treated as a list entry not a subtree. Without tagging the list operations like flatten and tree traversal become much more difficult. Mar 24, 2010 at 15:42
  • Right, in that case I second your solution with pseudo-typing as it's a way of building your own way of control flow.
    – Weasel
    Mar 25, 2010 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.