<br>Hi Tim,<br><br>There is a problem in your map/2 function.<br>The call to list_to_binary/1 makes it non-tail recursive,<br>so the recursion cannot reuse the stack frame.<br>Also, you will get many unnecessary calls to list_to_binary()
<br>(one per iteration).<br><br>One could instead write like this<br><br>map(F, Bin) -><br> list_to_binary(map_1(F, Bin))<br><br>map_1(F, <<A:8, Rest/binary>>) -> [F(A)|map_1(Rest)];<br>map_1(_, <<>>) -> [].
<br><br>In your map, the terminating case returned <<>>, which <br>would be right for a call to map(F, <<>>), but when <br>ending the iteration creates a non-proper list, e.g.<br>[$a,$b,$c|<<>>]. This is tolerated by list_to_binary(),
<br>but you should be aware that it is happening, because<br>it can bite you in other situations.<br><br>In OTP R11B-4, there is support for binary comprehensions,<br>even though it's still experimental. With it, I believe your
<br>function could be done in this fashion (haven't tried it <br>myself, the syntax might not be quite correct):<br><br>map(F, Bin) -><br> << F(C):1 || C:1 <<- Bin >>.<br><br>BR,<br>Ulf W<br><br>
<div><span class="gmail_quote">2007/4/17, Tim Becker <<a href="mailto:tim.becker@gmx.net">tim.becker@gmx.net</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br><br>I'm just starting out and still a little bit intimidated about how to<br>do things correctly in erlang and would like some advice. (In other<br>words, please bear with me.) I'd like to write some routines to recode
<br>charsets and I'm not sure what would be the best way to go about it.<br>What I've come up with so far is:<br><br> map (Function, <<A:8, Rest/binary>>) -> list_to_binary([Function(A)|<br>map (Function, Rest)]);
<br> map (_Function, <<>>) -> <<>>.<br><br> convert({ebcdic, iso_8859_1}, <<Binary/binary>>) -> map(fun<br>cp037_to_iso8859_1/1, Binary);<br> (...)<br> convert({From, To}, <<_Binary/binary>>) -> {unsupported_encoding, From, To}.
<br><br> cp037_to_iso8859_1 (0) -> 0;<br> (...)<br> cp037_to_iso8859_1 (129) -> 97; % a<br> cp037_to_iso8859_1 (130) -> 98; % b<br> cp037_to_iso8859_1 (131) -> 99; % c<br> cp037_to_iso8859_1 (132) -> 100; % d
<br> cp037_to_iso8859_1 (133) -> 101; % e<br> (...)<br> cp037_to_iso8859_1 (255) -> 159.<br><br><br>which works, so I'm relieved. But I'm not sure about the most<br>appropriate way to write the actual conversion functions. Using
<br>pattern matching for each conversion like above, or just having one<br>function body with a bunch of if's in it or a case statement...<br><br>>From the languages I'm used to, I'd probably use a bunch of arrays and
<br>have the value of the `from` charset be an index into the conversion<br>array. This would be possible as well, though it wouldn't be possible<br>to convert multibyte charsets like UTF-8...<br><br>Thanks in advance and sorry about the newbie question,
<br> -tim<br>_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions">
http://www.erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><br>