logical-purity

Features of good Prolog code? [closed]

烈酒焚心 提交于 2019-11-26 16:10:23
问题 What are the design heuristics one has to master to write good Prolog? I've heard it takes an experienced programmer about two years to become proficient in Prolog. Using recursion effectively is part of it, but that seems to be a relatively minor hurdle. What exactly is it that gives programmers so much trouble? What should I be looking for in sample code to judge its quality? 回答1: The major difficulty in writing good Prolog code lies in not only understanding but also adequately

Using \==/2 or dif/2

爷,独闯天下 提交于 2019-11-26 07:49:40
问题 If I want to make sure that two variables do not instantiate to the same term, what is the preferred way to do it? Let\'s say I need to find directed edges in a graph, and a node cannot have an edge to itself: node(a, x, y). node(b, z, x). node(c, y, y). (the edges here are a -> c, b -> a, but not c -> c) The following works: edge(A, B) :- node(A, _, X), node(B, X, _), A \\== B. This works too [swi-prolog]: edge(A, B) :- dif(A, B), node(A, _, X), node(B, X, _). This does not work, apparently