質問

私の努力が払われてきたが、現在、再帰Prologプログラムへのリンク経路の構成 基本 GPSのバーミンガムます。現在では出力としてい:

入力

routeplan(selly_oak, aston, P).

出力

P = [selly_oak, edgbaston, ... , aston]

何をしたいと思っていって違いますので、私のプログラムを何らかのインタフェースだった類のもの:

Route from selly_oak to aston

を提供できう:

Go from selly_oak to edgbaston
Go from edgbaston to ...
Finally, Go from ... to aston.

Prologには強力な言語になっているものと推察されるが容易に可能ですが、多くの書かれそうにスキップします。と認識していって使う、write()およびread()が詳細は不明です。

が誰にでもこのProlog初心者とも基本的な事例にリンクします。

編集: 多くのこれらの回答などの見所も複雑なの溶液は約5-10を行います。閲覧でなければならない問題という、日:

find:- 
    write('Where are you? '), 
    read(X), 
    nl, write('Where do you want to go? '),
    read(Y), 
    loopForRoute(X,Y).

思い出力されれば、書くことができ用write()で新しいライン(nl)使用できますが、その表示のように出力する。

このた私の入力を、あなたは何点ぐらいになると思うのトップrouteplan()これらの入力?また、私を追加し、ラインこれらの局ることもあるかもしれないパラメータにはどうするデータエスクローエージェントすべてのリンクが最初に定義ファイルのファイルはこのように:

rlinks(selly_oak, edgbaston, uob_line).
rlinks(edgbaston, bham_new_street, main_line).

このため、この情報をもとにいることができるラインを読むとしています。

Go from selly_oak to edgbaston using the uob_line
Go from edgbaston to ... using the ...
Finally, go from ... to aston using the astuni_line
役に立ちましたか?

解決

このようなもの、だいたいシェルの作成述語.そこで例---

guided:-
    print('Enter your start point'),nl,
    read(Start),
    print('Enter your destination'),nl,
    read(Dest),
    routeplan(Start, Dest, Route),
    print_route(Route).

とprint_route/1が再帰のようになります:

print_route([]).

print_route([[A,B,Method]|Tail]):-
    print_route(Tail),
    print('Go from '), print(A),
    print(' to '), print(B),
    print(' by '), print(Method), nl.

私はこの3変数のrouteplan/3述語の一覧のリストが表示されます。まるで築いたのが尾になります。場合ではないので比較的容易に対応.お願いします。

他のヒント

予約がついてなど詳細は 自然言語処理のためのPrologプログラマー によるマイケル-Aコヴィントン.

一般的に、何を行う必要があり

  1. Tokenize()の入力
  2. 解析のトークンなどとDCG)の入力 routeplan/3
  3. 電話 routeplan/3
  4. 生英語基に出力 routeplan/3

のようなこの作品SWI-Prolog):

% Usage example:
%
% ?- query_to_response('Route from selly_oak to aston', Response).
%
% Response = 'go from selly_oak to edgbaston then go from edgbaston
%         to aston then stop .'
%
query_to_response(Query, Response) :-
    concat_atom(QueryTokens, ' ', Query), % simple tokenizer
    query(path(From, To), QueryTokens, []),
    routeplan(From, To, Plan),
    response(Plan, EnglishTokens, []),
    concat_atom(EnglishTokens, ' ', Response).

% Query parser
query(path(From, To)) --> ['Route'], from(From), to(To).
from(From) --> [from], [From], { placename(From) }.
to(To) --> [to], [To], { placename(To) }.

% Response generator
response([_]) --> [stop], [.].
response([From, To | Tail]) -->
    goto(path(From, To)), [then], response([To | Tail]).
goto(path(From, To)) --> [go], from(From), to(To).

% Placenames
placename(selly_oak).
placename(aston).
placename(edgbaston).

% Mock routeplan/3
routeplan(selly_oak, aston, [selly_oak, edgbaston, aston]).

Hm、理解していますが正しくんをフォーマットのリストアなどの印刷いもんです。

にSWI-Prologこの作品:

output_string([A,B],StrIn,StrOut) :-
 concat_atom([StrIn, 'Finally, Go from ', A, ' to ', B, '.'],StrOut),
 write(StrOut).

output_string([A,B|Rest],StrIn,StrOut) :-
 concat_atom([StrIn,'Go from ', A, ' to ', B, '.\n'],StrAB),
 output_string([B|Rest],StrAB,StrOut).

その話

output_string(P,'',_).

あるんじゃないでしょうかは非常に効率的な仕事です。:)

ここにいくつかの例があります述語の読みラインからのファイル/ストリームに文字列Prolog:

%%% get_line(S, CL): CL is the string read up to the end of the line from S.
%%% If reading past end of file, returns 'end_of_file' in CL first, raises
%%% an exception second time.
%%% :- pred get_string(+stream, -list(int)).
get_line(S, CL) :-
    peek_code(S, C),
    (   C = -1
    ->  get_code(S, _),
        CL = end_of_file
    ;   get_line(S, C, CL)).

get_line(_, -1, CL) :- !, CL = [].  % leave end of file mark on stream
get_line(S, 0'\n, CL) :- !,
    get_code(S, _),
    CL = [].
get_line(S, C, [C|CL]) :-
    get_code(S, _),
    peek_code(S, NC),
    get_line(S, NC, CL).

%% read_lines(L): reads lines from current input to L.  L is a list of list
%% of character codes, newline characters are not included.
%% :- pred read_lines(-list(list(char))).
read_lines(L) :-
    current_input(In),
    get_line(In, L0),
    read_lines(In, L0, L).

%% read_lines(F, L): reads lines from F to L.  L is a list of list of character
%% codes, newline characters are not included.
%% :- pred read_lines(+atom, -list(list(char))).
read_lines(F, L) :-
    fail_on_error(open(F, read, S)),
    call_cleanup((get_line(S, L0),
              read_lines(S, L0, L)),
             close(S)).

read_lines(_, end_of_file, L) :- !, L = [].
read_lines(S, H, [H|T]) :-
    get_line(S, NH),
    read_lines(S, NH, T).

そしてこれらを実現するために、 DCGsについての情報解析のために文字列になります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top