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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With