2017-03-07 85 views
0

我得到了太多的結果。怎麼了?在prolog中剪切結果不存在?找到最大的樹

(deffacts mytree 
    (below birch poplar) 
    (above linden maple) 
    (below pine fir) 
    (below linden birch) 
    (above pine poplar)) 


(defrule high-low-tree 
    (below ?tree1 ?tree2) 
    (not (above ?tree1 ?tree2))  

    (or (above ?tree2 ?tree1) 
(not (above ?tree2 ?tree1))) 
    => 
    (printout t "The tallest tree " ?tree2 crlf) 
    (printout t "The lowest tree " ?tree1 crlf)) 

回答

1

尋找最高和最低:

CLIPS> 
(deffacts mytree 
    (tree birch) 
    (tree poplar) 
    (tree linden) 
    (tree maple) 
    (tree pine) 
    (tree fir) 
    (below birch poplar) 
    (above linden maple) 
    (below pine fir) 
    (below linden birch) 
    (above pine poplar)) 
CLIPS> 
(defrule high-low-tree 
    (tree ?tallest) 
    (tree ?lowest) 
    (not (below ?tallest ?)) 
    (not (above ? ?tallest)) 
    (not (below ? ?lowest)) 
    (not (above ?lowest ?)) 
    =>   
    (printout t "The tallest tree " ?tallest crlf) 
    (printout t "The lowest tree " ?lowest crlf)) 
CLIPS> (reset) 
CLIPS> (run) 
The tallest tree fir 
The lowest tree maple 
CLIPS> 

排行他們都:

CLIPS> (clear) 
CLIPS>  
(deffacts mytree 
    (tree birch) 
    (tree poplar) 
    (tree linden) 
    (tree maple) 
    (tree pine) 
    (tree fir) 
    (below birch poplar) 
    (above linden maple) 
    (below pine fir) 
    (below linden birch) 
    (above pine poplar) 
    (tree-list)) 
CLIPS> 
(defrule order-trees 
    (tree ?tallest) 
    ?t <- (tree-list $?list) 
    (test (not (member$ ?tallest ?list))) 
    (not (above ?above&:(not (member$ ?above ?list)) ?tallest)) 
    (not (below ?tallest ?below&:(not (member$ ?below ?list)))) 
    =>   
    (retract ?t) 
    (assert (tree-list ?list ?tallest))) 
CLIPS>  
(defrule print-list 
    (declare (salience -10)) 
    (tree-list $?list) 
    => 
    (printout t "Trees (tallest to lowest): " (implode$ ?list) crlf)) 
CLIPS> (reset) 
CLIPS> (run) 
Trees (tallest to lowest): fir pine poplar birch linden maple 
CLIPS> 
+0

太好了!謝謝你,先生。 –