2 ms·
A contrived basic example, if you want to validate that a number is between 100 and 150, and is a multiple of 5 then you can write this in Prolog or something v
by jodrellblank 5y ago
A contrived basic example, if you want to validate that a number is between 100 and 150, and is a multiple of 5 then you can write this in Prolog or something very like it in Python:
:- use_module(library(clpfd)).
valid(Num) :-
Num in 100..150,
Num mod 5 #= 0,
label([Num]).
# Python
def valid(num):
return (num in range(100, 151)) and (num % 5 == 0)
Then testing it, `valid(120)` is true, `valid(121)` is false. What the Prolog one does that the Python one doesn't do is if you pass in a placeholder, it can search for numbers that fit the cosntraints and output them, which is cool and a different focus on the problem and what the code exists to do. The Prolog ideal (not always easy to attain) is that you describe the patterns and constraints around the solution and Prolog finds the solution.
len(some_list) in Python can tell you the length of a list. length(Somelist, Len) in Prolog can tell you the length of a list, check if a list is the given length, generate a list of a specified length containing placeholders, or generate lists of increasing lengths containing placeholders. All languages can do what all other languages can do with enough effort; Prolog brings to the foreground relations and constraints and patterns instead of state manipulation and variable assignment.