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

  • Home
  • SEARCH
  • 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 8801001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:43:55+00:00 2026-06-14T00:43:55+00:00

We have a relatively simple assignment that I understand in theory but I think

  • 0

We have a relatively simple assignment that I understand in theory but I think I just don’t quite understand Prolog’s syntax enough to get that into code. Basically, we have a list of English notations that represent operations in C. They’re stored as a list when they’re passed to our Prolog program. For example:

add 4 to 3

is

[add, 4, to, 3]

We need to write a function that takes that list an returns the equivalent. So if I called

english2C([add,4,to,3], C).
C = 4+3

It would bind C to the result. So the data structure itself would be something like +(4(3)). We have a list of such English notation we have to translate, so it’s a finite number. It’s not like we have to account for all possibilities. There are also combinations, where they take two operations and combine them (with a comma in between)

english2C([add,3,to,5,',',then,subtract,7], C).
C = 3+5-7

I’m just somewhat confused as to how to start. I know I can take the very first element of the list and that will always be an operator (+,-,*, etc etc) and then I can just recursively go through the list looking for the operands. The problem there is for things that require order of operations, like “add 3 to 5 then multiply by 4”, which should be represented as (3+5)*4 but if you just translate it directly you get 3+5*4.

Oh and we have to see if we can get it to run backwards (give it a C statement (3+5) and translate back to english (add 3 to 5)). That part I don’t really have an idea for at all.

EDIT: There’s a large enough permutations of possible English notations that I can’t just pattern match everything. I get the idea that what I need to do is match the first operator with it’s corresponding arithmetic symbol then find the operands. For a combinational statement, that would be the first part (so I would have 3+5) and then there would be a comma followed by the next statement. By the way, the combinational statements can be as long as they want, so it’s not just two statements and I’m done.

  • 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-14T00:43:56+00:00Added an answer on June 14, 2026 at 12:43 am

    If there is a reasonable small number of patterns, you could do:

    english2C([add,X,to,Y], R) :- R is X+Y.
    english2C([add,A,to,B,',',then,subtract,C], R) :- R is A+B-C.
    

    edit

    Those rules above compute the value. To translate, we can use DCG for matching, it’s working ‘backwards’ as well.

    english2C(In, R) :- phrase(toe(R), In, []).
    
    toe(X+Y) --> [add,X,to,Y].
    toe(X*Y) --> [multiply,X,by,Y].
    toe(L-R) --> toe(L), [',',then,subtract,R].
    

    test:

    ?- english2C(X,3+6).
    X = [add, 3, to, 6].
    

    edit sorry, I forgot a cut. With that added, I get

    ?- english2C([add,3,to,5,',',then,subtract,4],X).
    X = 3+5-4.
    
    ?- english2C(L,3+5-4).
    L = [add, 3, to, 5, ',', then, subtract, 4].
    

    without, there is the loop after ;

    ?- english2C([add,3,to,5,',',then,subtract,4],X).
    X = 3+5-4 ;
    ^CAction (h for help) ? goals
    [698,875] toe(_G2096630, [add, 3, to, 5, ',', then, subtract, 4], _G2096652)
    [698,874] toe('<garbage_collected>', '<garbage_collected>', _G2096652)
    ...
    

    It’s a single point change: do you prefer to find it yourself?

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

Sidebar

Related Questions

I think this is a relatively simple question, but I don't precisely know what's
I have a relatively simple (I think) use-case but I can't find any examples
I have a relatively simple object with a method that makes an HTTP request.
I have a relatively simple question. Will using PHP guarantee that a form is
I have a relatively simple question that I cannot seem to find the answer
I have this relatively simple regex for usernames // Enforce that username has to
I have a relatively simple ASP.net MVC 2 app that is using SubSonic. Everything
I have a relatively simple example of an MVC page that is using jQuery
I have a relatively simple page that has a couple of LI entries that
I have a relatively simple program that currently uses 1 thread (main). The application

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.