An interesting problem that I recently came across is this one from learnyousomeerlang.com. The problem is to calculate the shortest distance to the end nodes in the grid. Here how I solved it using Erlang:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(heathrow). | |
-compile([debug_info, export_all]). | |
mainFunc() -> | |
BottomGraphVal = {{0,[]},{0,[]}}, | |
MoveCosts = [{50, 10, 30}, {5, 90, 20}, {40, 2, 25}, {10, 8,0}], | |
AccIn = {BottomGraphVal, [], []}, | |
RetVal = lists:foldl( fun(X,Acc) -> | |
{AMove, BMove, XMove} = X, | |
{{{AVal,AMoves}, {BVal,BMoves}}, AMovesList, BMovesList} = Acc, | |
Res = calcNewSetAndMoves({{AVal, AMoves}, {BVal, BMoves}}, AMove, BMove, XMove), | |
{{NewASetVal, NewAMoves}, {NewBSetVal, NewBMoves}} = Res, | |
{{{NewASetVal, NewAMoves}, {NewBSetVal, NewBMoves}}, [lists:reverse(NewAMoves) | AMovesList], [lists:reverse(NewBMoves) | BMovesList]} | |
end, AccIn, MoveCosts), | |
{{{AVal, APath}, {BVal, BPath}}, _, _} = RetVal, | |
{{AVal, BVal}, lists:reverse(APath), lists:reverse(BPath)}. | |
calcNewSetAndMoves(CurValsMoves, AFwd, BFwd, XCross) -> | |
{{AVal, AMoves}, {BVal, BMoves}} = CurValsMoves, | |
ValToAStraightFromA = AVal + AFwd, | |
ValToACrossFromB = BVal + BFwd + XCross, | |
NewASetVal = erlang:min(ValToAStraightFromA, ValToACrossFromB), | |
NewAMoves = | |
if ValToAStraightFromA < ValToACrossFromB -> | |
[aFwd | AMoves]; | |
true -> % works as an else branch | |
[xCross , bFwd | BMoves] | |
end, | |
%------------------------- | |
ValToBStraightFromB = BVal + BFwd, | |
ValToBCrossFromA = AVal + AFwd + XCross, | |
NewBSetVal = erlang:min(ValToBStraightFromB, ValToBCrossFromA), | |
NewBMoves = | |
if ValToBStraightFromB < ValToBCrossFromA -> | |
[bFwd | BMoves]; | |
true -> % works as an else branch | |
[xCross, aFwd | AMoves] | |
end, | |
{{NewASetVal, NewAMoves}, {NewBSetVal, NewBMoves}}. |
After having solved with Erlang, my second attempt was FSharp. I expected that these two could be very similar due to their shared functional paradigm, and indeed that came out fairly clearly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Move = | |
| AFwd | |
| BFwd | |
| XCross | |
// list of amove, bmove, xcross moves tuples (costs) | |
let moveCosts = [(50, 10, 30); (5, 90, 20); (40, 2, 25); (10, 8,0)] | |
let bottomGraphVal = ((0, ([]: Move list)), (0, ([] : Move list))) | |
let calcNewSetAndMoves ((aVal, (aRoute)), (bVal, (bRoute))) (aFwd, bFwd, xCross) = | |
let valToAStraightFromA = aVal + aFwd | |
let valToACrossFromB = bVal + bFwd + xCross | |
let newAVal = min valToAStraightFromA valToACrossFromB | |
let newAMoves = | |
if valToAStraightFromA < valToACrossFromB then AFwd :: aRoute | |
else XCross :: BFwd :: bRoute | |
//----------------------------------- | |
let valToBStraightFromB = bVal + bFwd | |
let valToBCrossFromA = aVal + aFwd + xCross | |
let newBVal = min valToBStraightFromB valToBCrossFromA | |
let newBMoves = | |
if valToBStraightFromB < valToBCrossFromA then BFwd :: bRoute | |
else XCross :: AFwd :: aRoute | |
((newAVal, newAMoves), (newBVal, newBMoves)) | |
let myTestRes = calcNewSetAndMoves bottomGraphVal (50, 10, 30) | |
let foldAccFn = | |
fun (curState) (curEntry ) -> | |
calcNewSetAndMoves curState curEntry | |
let main = | |
let retVal = List.fold foldAccFn bottomGraphVal moveCosts | |
let ((aVal,aList),(bVal,bList)) = retVal | |
(aVal, List.rev aList, bVal, List.rev bList) |
I also decided to solve the problem with CSharp, which has a less explicit functional orientation. I kept the main approach from the Erlang and FSharp implementations, which may have contributed to the CSharp solution not being very idiomatically coded. Any suggestions on how you would have gone about the problem solving in CSharp differently are most welcome (please comment on the post)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace CSHeathrow | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var init = new RoutePair(0, new List<string>(), 0, new List<string>() ); | |
Functions.CalcNewRoutePair(init, 50, 10, 30); | |
var moveSteps = new List<MoveStep> | |
{ | |
new MoveStep(50, 10, 30), | |
new MoveStep(5, 90, 20), | |
new MoveStep(40, 2, 25), | |
new MoveStep(10, 8, 0) | |
}; | |
var myResult = moveSteps.Aggregate(init, | |
(curState, curEntry) => | |
Functions.CalcNewRoutePair(curState, curEntry.AFwd, curEntry.BFwd, curEntry.XCross)); | |
Console.WriteLine("A Value: {0}", myResult.AVal); | |
Console.WriteLine("A Path: "); | |
foreach (var str in myResult.AMoves) | |
{ | |
Console.WriteLine(str); | |
} | |
Console.WriteLine(); | |
Console.WriteLine("B Value: {0}", myResult.BVal); | |
Console.WriteLine("B Path: "); | |
foreach (var str in myResult.BMoves) | |
{ | |
Console.WriteLine(str); | |
} | |
Console.WriteLine(); | |
} | |
} | |
class MoveStep | |
{ | |
public int AFwd { get; private set; } | |
public int BFwd { get; private set; } | |
public int XCross { get; private set; } | |
public MoveStep(int aFwd, int bFwd, int xCross) | |
{ | |
this.AFwd = aFwd; | |
this.BFwd = bFwd; | |
this.XCross = xCross; | |
} | |
} | |
class RoutePair | |
{ | |
public int AVal { get; private set ; } | |
public List<string> AMoves { get; private set; } | |
public int BVal { get; private set; } | |
public List<string> BMoves { get; private set; } | |
public RoutePair(int aVal, List<string> aMoves, int bVal, List<string> bMoves) | |
{ | |
this.AVal = aVal; | |
this.AMoves = aMoves; | |
this.BVal = bVal; | |
this.BMoves = bMoves; | |
} | |
} | |
class Functions | |
{ | |
public static RoutePair CalcNewRoutePair(RoutePair currentPair, int aVal, int bVal, int xCrossVal) | |
{ | |
var valToAStraightFromA = currentPair.AVal + aVal; | |
var valToACrossFromB = currentPair.BVal + bVal + xCrossVal; | |
var newAVal = Math.Min(valToAStraightFromA, valToACrossFromB); | |
List<string> newAMoves; | |
if(valToAStraightFromA < valToACrossFromB) | |
{ | |
newAMoves = new List<string>(currentPair.AMoves); | |
newAMoves.Add("AFwd"); | |
} | |
else | |
{ | |
newAMoves = new List<string>(currentPair.BMoves); | |
newAMoves.Add("BFwd"); | |
newAMoves.Add("XCross"); | |
} | |
//---------------------------------------------- | |
var valToBStraightFromB = currentPair.BVal + bVal; | |
var valToBCrossFromA = currentPair.AVal + aVal + xCrossVal; | |
var newBVal = Math.Min(valToBStraightFromB, valToBCrossFromA); | |
List<string> newBMoves; | |
if (valToBStraightFromB < valToBCrossFromA) | |
{ | |
newBMoves = new List<string>(currentPair.BMoves); | |
newBMoves.Add("BFwd"); | |
} | |
else | |
{ | |
newBMoves = new List<string>(currentPair.AMoves); | |
newBMoves.Add("AFwd"); | |
newBMoves.Add("XCross"); | |
} | |
return new RoutePair(newAVal, newAMoves, newBVal, newBMoves); | |
} | |
} | |
} |