'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.


Solution 1:[1]

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").

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TessellatingHeckler