seenu9333
seenu9333

Reputation: 115

what is the difference between [string()] and list() in erlang?

Erlang: what is the difference between [string()] and list() ??

I saw them as return types of ct_telnet:cmd and ct_ssh:exec ?

http://erlang.org/doc/man/ct_ssh.html

exec(SSH, Command, Timeout) -> {ok, Data} | {error, Reason}
Types:
  Data = list()

http://erlang.org/doc/man/ct_telnet.html

cmd(Connection, Cmd, Opts) -> {ok, Data} | {error, Reason}
Types:
  Data = [string()]

Upvotes: 3

Views: 453

Answers (2)

Nalin Ranjan
Nalin Ranjan

Reputation: 1782

Formally there is no such type as "string" in Erlang, however strings are denoted using a list of codes. So essentially

String() -> [Positive_Integer()] (list of positive integers)

[String()] -> [[Positive_Integer()]] (list of list of positive integers)

where [] denotes a list.

Upvotes: 0

legoscia
legoscia

Reputation: 41528

The type list() stands for any list, without specifying the type of its elements. Another way to write it is [_] or [term()].

A string() is a special case of list(): it is a list containing integers representing Unicode code points (or Latin-1 characters, if less than 256, or ASCII characters, if less than 128). Another way to write string() is list(char()) or [char()].

A [string()] is a list of strings. This type can also be written as list(string()). Since it is a special case of a list, it is also valid (though less informative) to write it as list().

Upvotes: 9

Related Questions