StreamsGit
StreamsGit

Reputation: 91

how to add value to existing variable in Prolog?

addGPA(GPA, TotalGPAS):-
            TotalGPAS = GPA + TotalGPAS.                            

I used this way, and it doesn't work. it says that is "Free variable in expression" so I don't know what to do.

Upvotes: 0

Views: 529

Answers (1)

Duda
Duda

Reputation: 3736

If you want to calculate arithmetic terms use the predicate is. This only works if all the variables on the right side are instantiated.

You cannot overwrite variables. If you want to have a different value, use a different variable. And this variable needs to be an additional attribute if you want to use it outside of the current call.

Result:

addGPA(GPA, TotalGPAS, NewTotal):-
            NewTotal is GPA + TotalGPAS.                            

Test

?- addGPA(1.7, 8.9, T).
T = 10.6.

Upvotes: 1

Related Questions