首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找到最大的树

找到最大的树
EN

Stack Overflow用户
提问于 2017-03-07 16:12:49
回答 1查看 48关注 0票数 0

我得到的结果太多了。你怎么了?在prolog中裁剪结果不存在吗?

代码语言:javascript
复制
(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))
EN

回答 1

Stack Overflow用户

发布于 2017-03-08 02:23:29

查找最高和最低:

代码语言:javascript
复制
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>

对他们全部进行排名:

代码语言:javascript
复制
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> 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42643263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档