سؤال

In prolog I have do to something like this:

    ************************
    *                      *
    *      ##########      *
    *      # button #      *
    *      ##########      *
    *                      *
    ************************

User gives the width and height of window (created with *), and coordinates, width, height and text of button (created by #). This "window" is written in prolog console. I don't have trouble with writing the window itself but I don't know how to do it with something in it. Can anyone help me, I don't mean writing it for me, but even small guidenes will be helpfull.

هل كانت مفيدة؟

المحلول

Write it with the help of a DCG as a clean grammar.

Here is a start. More details, here.

:- use_module(library(double_quotes)).
:- set_prolog_flag(double_quotes,chars).

newline --> "\n".

pre --> "# ".

post --> " #".

button -->
   pre,
   "button",
   post.

Now you can query it on the toplevel:

?- phrase(button, Xs).
Xs = "# button #".

And you can print it out directly:

?- phrase(button, Xs), atom_chars(A, Xs), write(A).

نصائح أخرى

Look at format/2, which works like printf in C++

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top