문제

나는 현재 재귀적인 프롤로그 프로그램을 연구하여 경로를 함께 연결하여 기초적인 버밍엄 지역의 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 ()의 줄을 따라 무언가를 사용해야합니다.

여기에있는 사람이 몇 가지 기본 예제 나 추가 정보에 대한 링크와 함께 프롤로그 초보자가 될 수 있습니까?

편집하다: 이 답변 중 많은 사람들이 솔루션이 약 5-10 줄의 코드라면이어야하는 매우 복잡해 보입니다. 값으로 읽는 것은 다음과 같은 선을 따라 무언가를 할 수 있으므로 문제가되지 않습니다.

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

write ()를 사용하여 출력을 작성할 수 있으므로 새 라인 (NL)을 사용할 수 있으므로 위의 출력과 같은 표시를 선호합니다.

이것이 내 입력이라면 어떻게이 입력으로 작업하기 위해 상단 플랜 ()을 어떻게 정리할 것인가? 또한이 스테이션의 라인을 추가 매개 변수로 추가한다면 어떻게 구현됩니까? 모든 링크는 파일의 시작 부분에서 다음과 같이 정의됩니다.

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
도움이 되었습니까?

해결책

이런 종류의 것에 대해, 나는 보통 쉘 predicates를 만듭니다. 그래서 당신의 경우 ...

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.

RoutePlan/3 술어의 세 번째 변수는 목록 목록이라고 가정했습니다. 또한 꼬리에 추가하여 만들어졌습니다. 그렇지 않은 경우 적응하기가 상당히 쉽습니다. 의견에 물어보세요.

다른 팁

그러한 것들을 자세하게 논의하는 책은 다음과 같습니다 Prolog 프로그래머를위한 자연 언어 처리Michael A. Covington.

일반적으로, 당신이해야 할 일은입니다

  1. 입력을 토큰 화하십시오
  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]).

흠, 내가 당신을 올바르게 이해한다면 당신은 인쇄를 위해 목록을 멋지게 포맷하고 싶습니까?

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,'',_).

아마도 효율적이지는 않지만 일을합니다. :)

다음은 파일/스트림에서 프롤로그 문자열로 줄을 읽을 수있는 몇 가지 예측입니다.

%%% 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).

그런 다음 살펴보십시오 DCG문자열을 구문 분석하는 방법에 대한 정보.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top