Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern matching types in Prolog

Let's say I have a list looking like this:

List=[alpha(1,2),beta(3,4),gamma(4,1)]

Ok, so I want to make a certain pattern matching here... I know I can do:

Try=alpha(Y,Z).
    Try=alpha(1,2)
    Y=1
    Z=2

But I would like to do for example:

Try=X(Y,Z)
    X=alpha
    Y=1
    Z=2

...so that I can pass on the data to another predicate:

targetPredicate(Type,Value1,Value2):-
    Type=alpha
    ...

and then do something with it instead of having to make one help predicate for every type I might run into:

helpPredicate(Input):-
    Input=alpha(Value1, Value2),
    targetPredicateAlt(Value1, Value2).

helpPredicate(Input):-
    Input=beta(Value1, Value2),
    targetPredicateAlt(Value1, Value2).

helpPredicate(Input):-
    Input=gamma(Value1, Value2),
    targetPredicateAlt(Value1, Value2).

Is there any way to get around this or am I doomed to use a ton of help predicates?

like image 254
fast-reflexes Avatar asked May 14 '26 07:05

fast-reflexes


1 Answers

You can use the univ predicate =../2: Suppose you have Try=alpha(1,2), then

Try =..[Name, X, Y].

would yield Name = alpha, X = 1, Y = 2.

like image 116
gusbro Avatar answered May 17 '26 02:05

gusbro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!