Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 3-Prolog/day1-map-colouring.pl @ 103:98be775c533c default tip
An odd "non-determinism" example from StackOverflow
It is clever, but doesn't make much sense as to how it gets its results
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Jul 2019 13:44:13 +0100 |
parents | 005ae3fad18f |
children |
line wrap: on
line source
% Colouring is done by defining facts of what colours must be "different" % i.e. not adjacent. % Note that we define all combinations - reflexivity isn't assumed, because % these are facts and not rules. different(red, green). different(red, blue). different(green, red). different(green, blue). different(blue, red). different(blue, green). % We then use a rule with variables to see which facts fit for the variables % Note: With this approach, we need to manually define that each state must be % different from each other state. We've not actually encoded any concept of % adjacency, just a request for differences. % On the plus side, we only need to define it in one direction, because the % facts already cover reflexivity. colouring(Alabama, Mississipi, Georgia, Tennessee, Florida) :- different(Mississipi, Tennessee), different(Mississipi, Alabama), different(Alabama, Tennessee), different(Alabama, Mississipi), different(Alabama, Georgia), different(Alabama, Florida), different(Georgia, Florida), different(Georgia, Tennessee). % The bad side with this is that we need to call % "colouring(Alabama, Mississipi, Georgia, Tennessee, Florida)." to run it. % On the minus side, that seems like duplication. On the plus side, we could % technically use any variable name and apply it to any situation where % adjacency is the same, it's just that the example wanted to be explicit.