Search code examples
prologprolog-assert

Reading in input using Prolog


Sorry if this is obvious, but I've been learning prolog recently and am attempting to read in a data to use in a recommender system.

 gifter :- write('how much money?  '), read(money), nl,
          assert(will_spend(money)),
          write('Is the giftee classy?  '), read(classy), nl.

The previous code should read in the amount of money a user wishes to spend and then ask about the giftee's personality, however, only the first question is asked. It seems to get to the new line but not to asserting the predicate :

?- will_spend(30). [WARNING: Undefined predicate: will_spend/1']

Why is this, what am I doing wrong? Thanks in advance for the help.


Solution

  • gifter :- write('how much money?  '), read(Money), nl,
              assert(will_spend(Money)),
              write('Is the giftee classy?  '), read(Classy), nl,
              assert(classy :- Classy = 'yes').
    

    Then,

    ?- gifter.
    how much money?  127.
    
    Is the giftee classy?  yes.
    
    true.
    
    ?- classy.
    true.
    
    ?- will_spend(X).
    X = 127.
    

    Remember that read wants a period and a newline; also, variables should be capitalised.