Summing a number to the terms of a list

Mikael Karlsson <>
Tue Aug 12 17:23:27 CEST 2003


I think that the output formatter assumes that your list is a string
as long as the characters have a value less than 255.
You can get the same format by using explicit formatting io:format...
 
I do not get the same output result for A,200 in my shell though:

16> lists:map(fun(A) -> 100+A end,[1,2,3]).
"efg"
17> lists:map(fun(A) -> 200+A end,[1,2,3]).
"ÉÊË"
18> lists:map(fun(A) -> 300+A end,[1,2,3]).
[301,302,303]
19> io:format("~w",[lists:map(fun(A) -> 100+A end,[1,2,3])]).
[101,102,103]ok
20> io:format("~w",[lists:map(fun(A) -> 200+A end,[1,2,3])]).
[201,202,203]ok
21> io:format("~w",[lists:map(fun(A) -> 300+A end,[1,2,3])]).
[301,302,303]ok

/Mikael

tisdag 12 augusti 2003 16:36 skrev Jilani Khaldi:
> Hi All,
>
> Why this code gives me these different results?
>
> -module(sumit).
> -export([sum_me/2]).
>
> sum_me(H|T,X) -> [(H+X)|sum_me(T,X)];
> sum_me([], X) -> [].
>
> Results:
>
> A=[1,2,3].
> sumit:sum_me(A,100).
> "efg"
>
> sumit:sum_me(A,200).
> "\311\312\313"
>
> sumit:sum_me(A,300).
> [301,302,303] -- as I expected.
>
> jilani




More information about the erlang-questions mailing list