0

just started programming with prolog and I'm having a few issues. I wanna store the result on an operation , for example:

transformer(kilo,1000).
transformer(hecto,100).
transformer(deca,10).
transformer(unite,1).
transformer(deci,0.1).
transformer(centi,0.01).
transformer(milli,0.001).
transformerT(sec,1).
transformerT(min,60).
transformerT(h,3600).

plus(V1,U,V2,U,UniteType,R,U) :-
  dif(UniteType,temps),R is V1+V2. 
plus(V1,U1,V2,U2,UniteType,R,unite) :-   
  dif(UniteType,temps), 
  dif(U1,U2),  
  trans(U1,Res1),  
  trans(U2,Res2),  
  R is V1*Res1+V2*Res2.

I want to store the result of this operation to call it later (like the ANS or M Buttons in a calculator) in another operation. Is it Possible?

David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
SimoDev
  • 1
  • 2

1 Answers1

0

If you want the information to survive a program termination (i.e. a return to the Prolog REPL, aka. toplevel) you can use predicates asserta/2 and assertz/2.

See this section for SWI Prolog, should be similar for SICStus: Database

Alternatively you may want to keep the program "alive" and store information in a term that is passed between predicates. Association lists library(assoc) or, for SWI Prolog, built-in dicts, or simpler data structure like lists can be used for that.

David Tonhofer
  • 14,559
  • 5
  • 55
  • 51