reset password
Author Message
rabbott
Posts: 1649
Posted 17:59 Apr 29, 2019 |

As you know, Prolog in Python makes heavy use of generators. We didn't spend enough time with generators -- and their off-spring co-routines. But as a brief example, here's code that generates all solutions to the 8-queens problem. (It finds 92 solutions, but many of them are rotations or mirror images of each other.)

It uses generators. It's also recursive, which means that when it finally finds a correct placement of 8 queens at the eighth level of recursion, it must send it back through yields to the top level. (yield works only at the level of the actual generator.) The easiest way to do that is to use the yield from construct, which yields whatever the argument to yield from yields. It's probably easier to look at the code to see what all that means.