Prolog: decrease, then write the value of the predicate

This is the code I'm writing

assert(bar(foo)),
assert(foo(bar-5)),

I am not sure if this works. I am trying to get it to reduce foo by 5. I need a way to write the value of foo, but also did not find a way. write('foo is' + foo)would be a logical way for me, but it doesn't seem to work.

+3
source share
1 answer

To be able to use a fact value, you must first combine it. Unification is performed by passing an unbound variable as an argument to the predicate, bar(Moo)in our case:

facts
    bar(integer)
    foo(integer)

goal
    assert(bar(42)),
    bar(Moo),
    Baz = Moo - 5,
    assert(foo(Baz)),
    write(Baz).
+1
source

Source: https://habr.com/ru/post/1745992/


All Articles