Well, once I got started, I kinda couldn’t help myself. I really should be debugging and cleaning up Mobtropolis, but sometimes, you need a break. To add to my shame of Erlang neophytism (probably not even a word), I tried out merge sort.
[Left, Right] = split(List, , ),
io:format("~w ~w~n", [Left, Right]),
split([Last], Acc1, Acc2)->
[ [Last | Acc1], Acc2 ];
split([First | Rest], Acc1, Acc2) when length(Acc1)
split(Rest, [First | Acc1], Acc2);
split([First | Rest], Acc1, Acc2) ->
split(Rest, Acc1, [First | Acc2]).
merge(, R) ->
merge(L, ) ->
merge([L_h | L_t], [R_h | R_t]) when R_h =
[R_h | merge([L_h | L_t], R_t)];
merge([L_h | L_t], [R_h | R_t]) ->
[L_h | merge(L_t, [R_h | R_t])].
Ugh. I thought mergesort would be my redemption, since in my mind, it looked shorter. But I ended up having to write split(), since I didn’t know the libraries well enough, and couldn’t think of a good list comprehension to split the list. Admittingly, it’s like deciding to forgo inventing the combustible engine and deciding to start walking–I just built it out what I little I knew. If you’re so inclined, you can also try one-liner-ing (also not a word) mergesort, as the bar I set was pretty low. 😦 Like Bryan says:
“Get back into Erlang! it’s good for you. ;)”
Speaking of Bryan, he posted a solution to bubblesort that was pretty good in the last post. It was a good lesson. Like anything worthwhile learning, languages are often easy enough to pick up, but hard to master. This was a good wakeup call to go through the examples in the book and master more Erlang before trying to write anything else on my own.