我有以下对PC进行建模的谓词。我要打印的是所有电脑配件和子配件。
我想做这样的事情
find_set_of_attributes(X):-
print(X)
print(X[power)
print(X[power][usb])
pc([power).
power([usb, voltageregulator]).
usb([container, usbhead, pins]).
voltageregulator([barreljack, capacitor, regulator, leds]).
答案 0 :(得分:1)
描述计算机组件和子组件的一种方法是使用定句语法(DCG)。例如:
computer --> monitor, cpu, keyboard, mouse.
...
power --> usb, voltageregulator.
usb --> container, usbhead, pins.
voltageregulator --> barreljack, capacitor, regulator, leds.
没有子组件的组件可以定义为:
capacitor --> [capacitor].
leds --> [leds].
要列出所有部分(例如,用于打印),您可以简单地使用事实上的标准phrase/2
谓词。例如:
| ?- phrase(computer, Parts).
答案 1 :(得分:0)
OP发布了一个有轻微问题的问题。由于计算机不包含power
进行更改,因此可以达到预期的效果
computer --> monitor, cpu, keyboard, mouse, power.
power --> usb, voltage_regulator.
usb --> container, usb_head, pins.
voltage_regulator --> barrel_jack, capacitor, regulator, led.
monitor --> [monitor].
cpu --> [cpu].
keyboard --> [keyboard].
mouse --> [mouse].
container --> [container].
usb_head --> [usb_head].
pins --> [pins].
barrel_jack --> [barrel_jack].
regulator --> [regulator].
capacitor --> [capacitor].
led --> [led].
Paulo建议使用词组/ 2
?- phrase(computer,Parts).
Parts = [monitor, cpu, keyboard, mouse, container, usb_head, pins, barrel_jack, capacitor, regulator, led].