Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if an item is a string or a list in Erlang

Tags:

erlang

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.
like image 312
yazz.com Avatar asked Mar 19 '10 18:03

yazz.com


1 Answers

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.

like image 118
Jeremy Wall Avatar answered Oct 29 '22 16:10

Jeremy Wall