如何在PROLOG中的谓词中查找列表内的值

问题描述 投票:1回答:1

到目前为止,我已经做了相当多的研究,并且尝试了不同的方法,但是即使在读取了多个堆栈溢出答案甚至是Addison Wesley的PDF之后,我仍然找不到解决方法。这是代码

use_module(library(func)).
% importing library "func"

scale([c, d, e, f, g, a, b]).
scale(c, major, [c, d, e, f, g, a, b]).
scale(c, minor, [c, d, e_b, f, g, a_b, b_b]).

%1st attempt 
search(note, scale):- scale(note, scale).

%2nd attempt
scaleOf(note, type_scale):- scale(note, type_scale).

on(item,[item|rest]).  

on(item,[disregardHead|tail]):-
    scale(tail),
    on(item, tail).

%3rd attempt 

fatherOf(father,type, son):- scale(father, type, sons), search(son, type, sons).
search(son, type, []):- !, fail.
search(son, type, [son|l]):- !, true.
search(son, type, [c|l]):- search(son, type, l).

我正在尝试什么?很简单,有些东西可以遍历谓词标度(c,[c,d,e,f,g,a,b])。但是我做错了。

编辑:我有多个谓词,因为其他人建议创建一个谓词,以将一个量表与另一个量表区分开。我以为可以在任何算法中都塞满它,但是我想PROLOG并不那么宽松:p

list search prolog predicate
1个回答
1
投票

您可以用member/2 [swi-doc]完成。它可用于搜索,与成员统一或生成列表。

© www.soinside.com 2019 - 2024. All rights reserved.