Skip to content

Instantly share code, notes, and snippets.

@jimscarver
Last active July 14, 2021 18:41
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 jimscarver/e6f5d64d024003a1f5f113f6d1703bc0 to your computer and use it in GitHub Desktop.
Save jimscarver/e6f5d64d024003a1f5f113f6d1703bc0 to your computer and use it in GitHub Desktop.
match [3] {
[height] => {
new result, stdout(`rho:io:stdout`), move, ack, log, list in { // create names/channels needed
// towers of hanoi - use EXPLORE
// derived from prolog example https://www.cpp.edu/~jrfisher/www/prolog_tutorial/2_3.html
// Move a pile of disks of [height] from the "left" peg to the "right" peg
// using the "center" peg as an intermetiate place to put them.
// Never put a larger disk on a smaller on.
// Then write to the ack channel to acknowledge completion
move!(height,"left","right","center", *ack) |
contract move(@height, @from, @to, @other, ack) = {
// create a new ack1 channel to sequence actions within move
new ack1 in {
match height {
1 => { // if the height is 1 move from the @from peg to the @to peg directly
log!("Move top disk from " ++ from ++ " to " ++ to, *ack1) |
stdout!("Move top disk from " ++ from ++ " to " ++ to) |
for (_ <- ack1) {ack!(Nil)}
}
_ => { // if the height is not one, uncover a larger disk from the @from onto the @other pile
move!(height-1, from, other, to, *ack1) |
for ( _ <- ack1 ) { // then move the larger disk to the @to pile
move!(1, from, to, other, *ack1) |
for ( _ <- ack1 ) { // then move the smaller [other] on top of the @to
move!(height-1, other, to, from, *ack1) |
for ( _ <- ack1 ) {
ack!(Nil) // when done return an acknowledgement
}
}
}
}
}
}
} |
list!([]) |
contract log(term, ack) = {
for ( prior <- list) {
list!(*prior ++ [*term]) |
ack!(Nil)
}
} |
for (_ <- ack; alist <- list ) {result!(*alist)}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment