[erlang-questions] 1st day of Erlang: what is wrong with this code

Kenneth Lakin kennethlakin@REDACTED
Sun Jul 12 12:11:04 CEST 2015


On 07/12/2015 01:40 AM, avinash D'silva wrote:
> I get an error for string:to_upper()

It would have been useful if you'd pasted the error in your message. :)

Anyway, the Data that is being returned by gen_tcp:recv/2 is being
returned as a binary, rather than a list. (I might be wrong here, but a
"list" is what Erlang calls non-Unicode strings.) string:to_upper/1
expects a list. If you look where TCP_OPTIONS is defined, you see that
one of the options passed to gen_tcp:listen/2 is "binary". Change that
to "list" and gen_tcp:recv will return lists, rather than binaries.

(See http://erlang.org/doc/man/gen_tcp.html#listen-2 for info on the
options you can pass to gen_tcp:listen/2 [and indeed, that entire page
for info on the gen_tcp module]. The docs are arranged intelligently.
This means that the reference manual for everything in the string module
is at http://erlang.org/doc/man/string.html )

Also -just for fun- before you change TCP_OPTIONS, add
io:format("~p~n", [Data]),
before the line
gen_tcp:send(Socket, string:to_upper(Data)),
then rebuild and restart your program. If you look carefully at the
output from the Erlang shell, you'll notice that binaries are printed
<<"Like this">> and -after you change TCP_OPTIONS- that lists are
printed "Like this".

I'm no expert, but I remember hearing that -for large amounts of data-
working with binaries is substantially faster than working with lists.
As this is your first day with Erlang, you probably don't need to worry
about that. :)

Also, check out "Learn you some Erlang for Great Good!" (
http://learnyousomeerlang.com/ ). You might get some value out of it.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150712/c529a70f/attachment.bin>


More information about the erlang-questions mailing list