Problems with Strings

Raimo Niskanen <>
Fri Mar 28 07:49:34 CET 2003

inversa("") -> "";
inversa([Ini|Cont]) -> Inv= inversa(Cont), [Ini|Inv].

You have accidentally built a deep list, which does not print as a 
string (beginner mistake no ~3). If you would have written your deep 
list to a file, or printed it with io:format("~s~n", [[$c,[$a,[$b]]]]), 
you would have got what you wanted.

And why not use lists:reverse/1,2 instead of writing your own? They use 
a built in emulator function that is much faster than any Erlang code.

Background: There are no strings in Erlang. They are represented as 
lists of character ascii values. "abc" is syntactical sugar for 
[$a,$b,$c]. If you print a list using format character ~p it will use 
the "abc" syntactical sugar style if the list is a flat list of 
printable characters.

/ Raimo Niskanen, Erlang/OTP

Pablo Dejuan wrote:
> Hi. I'm new in this and I have dificulties using strings. I tried to do 
> an "inverese" function that should turn "bac" into "cab" but instead of 
> this I get [98,[97,[99,[]]]] which I noticed that are tha Ascii codes 
> for c, a, and b.
> Here is the code:
> inversa("") -> "";
> inversa([Ini|Cont]) -> Inv= inversa(Cont), [Ini,Inv].
> Thanks In Advance.

More information about the erlang-questions mailing list