How to keep the first result of a function from Prolog?

Posted by zuhakasa on Stack Overflow See other posts from Stack Overflow or by zuhakasa
Published on 2014-06-04T14:58:33Z Indexed on 2014/06/04 15:25 UTC
Read the original article Hit count: 137

Filed under:
|

I need to write a customized function that will be called many times by other fixed functions. In this function, at the first called time, it will return the total number of lines of a file. The second called time of this function, forward, will return the number of lines in small sections of this file. My question is how I keep the first returned result(total number of lines of a file) and use it for the next called times of my function. I need to write or declare any thing only in this function(not in the caller). Something like this:

myFunction(Input, MyResult, FirstResult) :-
   calculateInputFunction(Input, Result),
   !,
   MyResult is Result,
   ... .

The problem is, every time myFunction is called, it receives different Input and returns different MyResult. But I would like to keep the first MyResult to use for next called times of myFunction. How can I do that? Thanks very much for your answer in advance.

© Stack Overflow or respective owner

Related posts about prolog

Related posts about logic