Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:00:26+00:00 2026-06-12T12:00:26+00:00

I am learning Prolog ideas and here is what I want to practice: I

  • 0

I am learning Prolog ideas and here is what I want to practice:

I want to write a Prolog program that can work like this:

?- input([apple,is,fruit]).
?- input([chicken,is,meat]).
?- input([Is,apple,meat]).
No, it is a fruit
?- input[(Is,chicken,meat])
Yes.

And when I was trying to implement this program, I got some problem:

(1) I used this code trying to read the input and distinguish between questions and assertions, but it fails:

input([]).
input([X|R]) :- X is 'Is', write('test code read question'); write("test code read assertion").

(2) I am still confused about How I can filter the useful information out from the input message. For example, in the [Apple,is,fruit]input array, all I need is apple and fruit. How do we normally do to jump the is word?

I don’t want to hardcode too many things to the program, and prefer a good functional programming style to solve the problem that can help me learn from it.

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:00:28+00:00Added an answer on June 12, 2026 at 12:00 pm

    Just because I enjoy them, I’d be inclined towards a definite clause grammar (DCG). Then you can make statements and parse them fairly easily:

    word(Word) --> [Word].
    statement(Statement) --> 
        word(Thing), [is], word(Category), 
        { Statement =.. [Category, Thing] }.
    

    The trick going on here with DCGs is that your grammar is being transformed to a difference list representation. The part in brackets is normal Prolog code; the part around it is interpreted either as literal parts of the list or as other grammar rules. So [Word] is matching one atom. We could actually write the rule like this:

    statement(Statement) -->
        [Thing, is, Category],
        { Statement =.. [Category, Thing] }.
    

    and it would have the same effect, and perhaps be more readable, but I like being pedantic. Note especially the use of =.., which converts lists into facts and vice versa (so [fruit, apple] becomes fruit(apple)).

    Parsing with DCGs is quite easy: use phrase/2:

    ?- phrase(statement(X), [apple,is,fruit]).
    X = fruit(apple).
    

    Then you can use asserta to insert these clauses into the dynamic store:

    input(Text) :- 
        phrase(statement(Statement), Text),
        asserta(Statement).
    

    For example:

    ?- input([apple,is,fruit]).
    true.
    
    ?- fruit(X).
    X = apple.
    

    Now you can write another clause for parsing queries:

    query(Query) --> 
        ['Is'], word(Word), word(Category), 
        { Query =.. [Category, Word] }.
    

    Looks very similar! And again, if you wanted, you could use this syntax:

    query(Query) -->
        ['Is', Word, Category],
        { Query =.. [Category, Word] }.
    

    Now you might want to write a clause to combine both grammar rules into one “sentence” rule:

    sentence(statement(S)) --> statement(S).
    sentence(query(Q))     --> query(Q).
    

    Try it out:

    ?- phrase(sentence(X), ['Is', apple, fruit]).
    X = query(fruit(apple)).
    
    ?- phrase(sentence(X), [apple, 'is', fruit]).
    X = statement(fruit(apple)) ;
    

    You see now we get not just the parsed fact, but also a wrapper that tells us whether it was a statement or a query. We can now parse with this instead of statement and query, and rewrite input like so:

    input(Text) :-
        phrase(sentence(S), Text), perform(S).
    

    I’ve added an auxiliary to handle the work:

    perform(statement(S)) :- asserta(S).
    perform(query(Q))     :- Q.
    

    This will be convenient later as you add more grammatical abstractions: each perform clause handles a different “sentence” type, so you do your parsing above and handle the work below. And now we have, more or less, what you wanted:

    ?- input([apple,is,fruit]).
    true ;
    false.
    
    ?- input(['Is',apple,fruit]).
    true.
    
    ?- input(['Is',banana,fruit]).
    false.
    

    You can improve things by introducing a cut in your sentence rule and handling true/false with special output, but I think this is the direction I’d want to go in, particularly if you want to be able to handle different syntax in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For learning purposes I'm building a simple messaging program that I think can work
learning html/css here. I can't seem to understand how to tweak this drop down
I am learning Prolog using SWI-Prolog. Here is my practice code in file fact.pl
Just learning the world of jquery, and all my googling gives examples like this:
Learning C and this is an exercise. The user is asked to input the
Learning sed, I want to change the drive letter of the following input lines
Learning C++ and see the class laid out like this: class CRectangle { int
While learning Prolog, I'm trying to solve the following problem, using accumulators: Write a
In the process of learning Prolog I'm making simple program which given some thing
I'm in the process of learning Prolog. I have this recursive predicate: add(0,Y,Y). add(succ(X),Y,succ(Z))

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.