Sunday, October 05, 2008

Once more with feeling… — A-star shortest path finder in Erlang

The same problem as in the previous post, only this time in Erlang:

Where I know that a recursion will be shallow, by construction, I've not attempted to write everything tail-recursively; that I've kept in the main A* algorithm, which should be generic enough to extract, and provide with extra function arguments.

The obvious effects of having to avoid state, with recursion rather than iteration, are more code and longer argument lists to the recursions. Still, an interesting exercise in rewriting the code, changing the way one thinks about the processing.


Of course, now I've had a comment, I can't just refactor in place, like I've done with the Python…


Unknown said...

Idiomatic Erlang code typically contains very few if any "if" statements, but here you've used them all over the place. A number of them can be replaced with matching on fun arguments, and others can be replaced with case statements. The result will be clearer code.

Steve Gilham said...
This comment has been removed by the author.
Steve Gilham said...

Thanks for the comment! (Yes, I was sneakily using the internet to get a code review, since I don't have any IRL contacts with folk more experienced in the language).

The two things that kept driving me to if were
a) testing a function of an argument (not pattern matchable)
b) usually a binary decision.

I shall have to give it another go-around.

Still stumped on a terse alternative to the if/else cascade type construct for bit-masking that isn't a convert-to-binary

Unknown said...

i won't rewrite *ALL* your code to not use if , but consider the print_swap function;

you have :

print_swap(Swap, Side) ->
Side =:= 0 ->
io:format( " ~s -->~n", [display(Swap)]);
true ->
io:format( "<-- ~s~n", [display(Swap)])

how about instead :

print_swap( Swap, 0 ) ->
io:format( " ~s -->~n", [display(Swap)]);

print_swap(Swap, _ ) ->
io:format("<-- ~s~n", [display(Swap)]).

how much cleaner is that? :)


Steve Gilham said...

See the next post