Created
May 29, 2012 18:41
-
-
Save henrik-ch/2829955 to your computer and use it in GitHub Desktop.
solution to the heathrow route planning problem from learnyousomeerlang.com
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment