<div dir="ltr">Thanks for your very informative answer.<br><br><div class="gmail_quote">On Mon, Oct 20, 2008 at 7:37 PM, Richard O'Keefe <span dir="ltr"><<a href="mailto:ok@cs.otago.ac.nz">ok@cs.otago.ac.nz</a>></span> wrote:<br>
<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"><br>
On 20 Oct 2008, at 5:56 pm, Edwin Fine wrote:<br>
</div><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">
I know it would be easy to write a pattern-matching-style function, but I wanted to do it without having to define a new function,<br>
<br></div><div class="Ih2E3d">
element(1, lists:mapfoldl(fun(_,[X,Y|T]) -> {{X,Y}, T} end, L, lists:seq(1, length(L) div 2))).<br>
</div></blockquote>
<br>
But you just defined a new function and a pattern-matching one<br>
at that.<br>
<br>
The obvious function is<br>
<br>
to_proplist([Key,Val|Rest]) -><br>
[{Key,Val} | to_proplist(Rest)];<br>
to_proplist([]) -><br>
[].<br>
<br>
Now let's pretend that you've heard of "unfold",<br>
maybe even read<br>
<a href="http://debasishg.blogspot.com/2007/11/fun-with-unfold-in-erlang.html" target="_blank">http://debasishg.blogspot.com/2007/11/fun-with-unfold-in-erlang.html</a><br>
</blockquote><div><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
Let's copy the unfold function from there.<br>
<br>
unfold(Seed, Predicate, Transformer, Incrementor) -><br>
unfold(Seed, Predicate, Transformer, Incrementor, []).<br>
<br>
unfold(Seed, Predicate, Transformer, Incrementor, Acc) -><br>
case Predicate(Seed)<br>
of false -> lists:reverse(Acc)<br>
; true -> unfold(Incrementor(Seed),<br>
Predicate,<br>
Transformer,<br>
Incrementor,<br>
[Transformer(Seed)|Acc])<br>
end.<br>
<br>
Now it's fairly straightforward:<br>
<br>
to_proplist(Even_List) -><br>
unfold(Even_List,<br>
fun (L) -> L =/= [] end,<br>
fun ([Key,Val|_]) -> {Key,Val} end,<br>
fun ([_,_|Rest]) -> Rest end).<br>
<br>
When you want to generate a list, an unfold is often what<br>
you want rather than a fold.<br>
</blockquote><div><br>
I'll pretend that you actually answered, and maybe even read, the
question I was asking. Somehow, Richard Carlsson managed to do that,
without inventing new library functions, so it couldn't have been too
obscure and badly-worded. However, I see now that I asked the wrong question, or phrased it incorrectly, so thank you for setting me straight. I apologize for not having heard of unfold, and for not having read every blog in existence that contained something relevant to my question prior to wasting people's valuable time on this list by asking it in the first place.<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I don't suppose we could have unfold/4 in the lists module,<br>
could we?<br>
<br>
</blockquote><div> </div></div></div>