序言,写没有原子的表达 [英] Prolog, writing expression with no atoms

查看:49
本文介绍了序言,写没有原子的表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Prolog的新手,我希望能够在很好地了解发生了什么情况的同时回答这个问题

New to Prolog and I want to be able to answer this question while getting a good understanding of whats going on

写一个谓词no_atoms/1,如果它的参数是一个没有任何原子.例如,no_atoms(5-(3 + x))应该失败,no_atoms((12 + 8)-7)应该会成功.

Write a predicate no_atoms/1 which succeeds iff its argument is an expression without any atoms. For example, no_atoms(5-(3+x)) should fail, no_atoms((12+8)-7) should succeed.

任何帮助将不胜感激!

推荐答案

您需要测试表达式不是原子.然后,使用compound/1进行检查,是否需要使用arg/3枚举参数,或者是否需要成功.

You need to test that the expression is not an atom. Then, you check with compound/1 if you need to enumerate the arguments with arg/3 or succeed.

no_atoms(Expr) :-
    \+ atom(Expr),
    (   compound(Expr)
    ->  \+ ( arg(_, Expr, A), \+ no_atoms(A) )
    ;   true
    ).

这篇关于序言,写没有原子的表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆