hanoi-spec Pub Share

Good recursion for the hanoi problem

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

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

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

$when:: sample.hanoi (disk, source, dest, aux)
   $ifc:: (disk > 0) sample.hanoi (disk=(disk - 1), source=source, dest=aux, aux=dest)
   $ifc:: (disk >= 0) sample.hanoi.move (disk=disk, source=source, dest=dest)
   $ifc:: (disk > 0) sample.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:: sample.hanoi.move (disk, source, dest)
   ctx.echo (ma=state[="a"..., mb=state[="b"..., mc=state[="c"...)
   . (x=state[=source][="0"=0])
   . (state[=source]=state[=source][:Range=1..None])
   . (state[=dest]=([x] + state[=dest]))


Was this useful?    

By: Razie | 2019-05-29 .. 2021-02-02 | Tags: spec , dsl , private


Viewed 944 times ( | History | Print ) this page.

You need to log in to post a comment!

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