hanoi-spec Pub

Good recursion for the hanoi problem

Declaring the main recursion message: this is not required, but it is good practice:

msg a.hanoi  (disk, source, dest, aux)

The recursive mechanism - stops at 0 and recurses before and after moving...

$when:: a.hanoi (disk, source, dest, aux)
   $ifc::(disk > 0) a.hanoi (disk<-(disk - 1), source<-source, dest<-aux, aux<-dest)
   $ifc::(disk >= 0) a.move (disk<-disk, source<-source, dest<-dest)
   $ifc::(disk > 0) a.hanoi (disk<-(disk - 1), source<-aux, dest<-dest, aux<-source)

Moving the discs around - notice the selectors. Also, remember that source/dist are local variables and their reference variables in the global context (state).

$when:: a.move (disk, source, dest)
   ctx.echo (ma<-state[="a"], mb<-state[="b"], mc<-state[="c"])
   . (x=state[=source][=0])
   . (state[=source]=state[=source][:Range=1..None])
   . (state[=dest]=(x + state[=dest]))


Was this useful?    

By: Razie | 2019-05-29 | Tags: spec , dsl


Viewed 89 times ( | Print ) this page.

You need to log in to post a comment!

© Copyright DieselApps, 2012-2020, all rights reserved.