Skip to content

Instantly share code, notes, and snippets.

@kunigami
Last active August 6, 2017 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunigami/e715a80869f324c2cbf1f42e6cd7a859 to your computer and use it in GitHub Desktop.
Save kunigami/e715a80869f324c2cbf1f42e6cd7a859 to your computer and use it in GitHub Desktop.
let nextState rotationStep =
match rotationStep with
| Reversing ({
validCount;
front = firstElem :: restFront;
frontReversed;
rear = lastElem :: restRear;
rearReversed;
}) -> Reversing ({
validCount = validCount + 1;
front = restFront;
frontReversed = firstElem :: frontReversed;
rear = restRear;
rearReversed = lastElem :: rearReversed;
})
| Reversing ({
validCount;
front = [];
frontReversed;
(* Invariant: restRear must be empty *)
rear = lastElem :: restRear;
rearReversed;
}) -> Appending ({
validCount;
front = frontReversed;
rear = lastElem :: rearReversed;
})
| Appending ({validCount = 0; front; rear}) -> Done rear
(* Transfer one element of front to rear *)
| Appending ({validCount; front = elem :: restFront; rear}) ->
Appending ({
validCount = validCount - 1;
front = restFront;
rear = elem :: rear;
})
| rotationStep -> rotationStep (* No-op *)
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment