<div dir="ltr">On Mon, Oct 20, 2008 at 3:06 AM, Richard Carlsson <span dir="ltr"><<a href="mailto:richardc@it.uu.se">richardc@it.uu.se</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">Edwin Fine wrote:<br>
> For example, the function would do this: f([1,2,3,4,5,6,7,8]) -><br>
> [{1,2},{3,4},{5,6},{7,8}].<br>
><br>
> I know it would be easy to write a pattern-matching-style function, but<br>
> I wanted to do it without having to define a new function, in as compact<br>
> a way as possible while still keeping it reasonably efficient.<br>
<br>
</div>element(1, lists:foldr(fun (X,{As,[]}) -> {As,[X]}; (X,{As,[Y]}) -><br>
{[{X,Y}|As],[]} end, {[],[]}, List)).<br>
<font color="#888888"></font></blockquote><div><br>That's beautiful - thanks! That's exactly what I was looking for - an idiom. I never thought of having two accumulators. I see that the principle you used could be extended to making triples instead of pairs, and so on:<br>
<br><span style="font-family: courier new,monospace;">element(1, lists:foldr(fun(Z,{As,[]}) -> {As,[Z]};<br> (Y,{As,[Z]}) -> {As,[Y,Z]};<br> (X,{As,[Y,Z]}) -> {[{X,Y,Z}|As],[]}<br>
end,<br> {[],[]},<br> List))</span><br style="font-family: courier new,monospace;"><br>Ulf, thanks for your response, too!<br><br></div></div>Regards,<br>
Edwin<br></div>