简体   繁体   English

如何在 prolog 中实现以下问题?

[英]How can I implement the below problem in prolog?

% Problem 1: implement get_age(Name, Age). This predicate
% returns in Age the age in 2022 of the student with the given Name.
% It assumes that student information is given in a program structure:  
% student(name(Name), born(Year)), where Year is the year of birth.
% It returns false if Name is not found.
% DO NOT USE ASSERT.  DO NOT USE ";".  Do not use write.

% Use this test data:
student(name(ann), born(2022)).
student(name(bob), born(2007)).

% Use these test cases to demonstrate your program:
% Note: Do not use the write predicate in this program.
% Let Prolog automatically output A=__ or false.
% ?- get_age(ann, A).
% A=0.
% ?- get_age(bob, A).
% A=15.
% ?- get_age(jacob, A).
% false.
get_age(Name, Age) :-
    student(name(Name), born(Year)),
    Age is 2022 - Year.

The predicate holds for any student with a Name and a birth Year, and Age is related to the birth year by the given calculation ("the age in 2022").该谓词适用于任何具有姓名和出生年份的学生,并且年龄通过给定的计算与出生年份相关(“2022 年的年龄”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM