prolog - 无法在 prolog 中显示输入和输出
问题描述
我是prolog的初学者有人可以帮我吗?
>go:-
write('Name?'),
read(Name), staff(Name, ID, Age),
write('ID?'),
read(ID), staff(Name, ID, Age),
write(Age).
staff(john, 123, 21).
staff(john, 222, 19).
staff(john, 333, 35).
ID提示不断重复
输出
?-go
Name?
john
ID?
333
ID?
333
ID?
333
35
解决方案
示踪剂显示正在发生的事情:
?- trace,go.
Call: (11) go ? creep -
Call: (12) write('Name?') ? creep -
Name? -
Exit: (12) write('Name?') ? creep -
Call: (12) read(_6414) ? creep -
|: john. - enter "john." (the term 'john' followed by a .)
-
Exit: (12) read(john) ? creep -
Call: (12) staff(john,_6508,_6510) ? creep - Prolog find the staff fact with 'john' as argument 1
Exit: (12) staff(john,123,21) ? creep - ID is now 123 and Age is now 21
Call: (12) write('ID?') ? creep -
ID? -
Exit: (12) write('ID?') ? creep -
Call: (12) read(123) ? creep - Prolog expects you to enter 123, the current value of ID.
|: 333. - Enter "333." (the term '333' followed by a ., which maps to an integer)
-
Fail: (12) read(123) ? creep - As 123 is not 333, reading is a failure!
Redo: (12) staff(john,_6508,_6510) ? creep - Backtracking! The earliest predicate that gives more solution is staff/3.
Exit: (12) staff(john,222,19) ? creep - The second solution is 'john' with ID 222 and age 19
Call: (12) write('ID?') ? creep -
ID? -
Exit: (12) write('ID?') ? creep -
Call: (12) read(222) ? creep - Prolog expects you to enter 222, the current value of ID.
|: 333. - Enter "222." (the term '222' followed by a ., which maps to an integer)
-
Fail: (12) read(222) ? creep - As 222 is not 333, reading is a failure!
Redo: (12) staff(john,_6508,_6510) ? creep - Backtracking! The earliest predicate that gives more solution is staff/3.
Exit: (12) staff(john,333,35) ? creep - The second solution is 'john' with ID 333 and age 35
Call: (12) write('ID?') ? creep -
ID? -
Exit: (12) write('ID?') ? creep -
Call: (12) read(333) ? creep - Prolog expects you to enter 333, the current value of ID.
|: 333. -
-
Exit: (12) read(333) ? creep - You entered 333 this time, reading is a success! Proceed!
Call: (12) staff(john,333,35) ? creep - Call staff(john,333,35).
Exit: (12) staff(john,333,35) ? creep - That fact exists, so succeess. Proceed.
Call: (12) write(35) ? creep - Write age value
35 -
Exit: (12) write(35) ? creep -
Exit: (11) go ? creep - The clause succeeds
true. -
这可能是您想要的,使用“失败驱动的循环”,从而将fail
您拉回到repeat
另一个尝试:
staff(john, 123, 21).
staff(john, 222, 19).
staff(john, 333, 35).
go:-
repeat,
format("Name?~n",[]),
read(Name),
format("ID?~n",[]),
read(ID),
(staff(Name, ID, Age)
-> format("Age of ~q with ID ~q is ~d~n",[Name,ID,Age])
; (format("No entry with Name ~q and ID ~q~n",[Name,ID]),fail)).
推荐阅读
- python-3.x - spacy 将带有数字的名称标识为 PERSON 实体
- javascript - 如何为每个对象将特定键值从一个对象添加到另一个对象数组
- javascript - 如何将类添加到标签
- javascript - firebase 数据库函数返回值
- javascript - 如何根据浏览器时间更改 iframe 源?
- sql - 从 SQL 中的日期值中删除时间戳
- unix - 我想开发一个 Rsync 脚本以递归方式将在一台服务器上创建的文件/文件夹列表复制到另一台服务器
- tfs - 维护两个不同的源代码控制服务器
- vue.js - 什么时候在 vue 中使用 prop 修饰符?
- c# - 图像未出现在所选项目组合框中