现在的位置: 首页 > 综合 > 正文

2008 October 20th Monday (十月 二十日 月曜日)

2013年05月12日 ⁄ 综合 ⁄ 共 1449字 ⁄ 字号 评论关闭
  Theseday, it is still hot.  It is unusually in autum.  In this season, people are easy to catch a cold.

repeat

  repeat generates an infinite sequence of backtracking choices. The purpose is to repeatedly perform some action on elements which are somehow generated, e.g.
by reading them from a stream, until some test becomes true. Repeat loops cannot contribute to the logic of the program. They are only meaningful if the action
involves side-effects. The only reason for using repeat loops instead of a more natural tail-recursive formulation is efficiency: when the test fails back, the
Prolog engine immediately reclaims any working storage consumed since the call to repeat/0.

  There is a useful program to write in prolog which is used to operate on a list.

% about list.
insert_element(X,L,[X|L]).

get_element([], _, nil) :- !.
get_element(_, 0, nil) :- !.
get_element(L, N, X) :-
    iter(L, 1, N, X, _).

iter([H|T], S, E, X, R) :-
    S < E,
    SS is S + 1,
    iter(T, SS, E, X, R).
iter([H|T], E, E, H, T).

get_rest_list([], _, []).
get_rest_list(L, 0, L).
get_rest_list(L, N, RL) :-
    iter(L, 1, N, _, RL).

get_range_list(E, E, _, []).
get_range_list(S, E, L, RL) :-
    S < E,
    get_rest_list(L, E, TL),
    %iter(L, 0, E, _, TL),
    append(SL, TL, L),
    iter(SL, 1, S, _, RL).

list_len([], 0).
list_len([H|T], N) :-
    list_len(T, NN),
    N is NN + 1.

get_head_list(L, TO, R) :-
    get_range_list(1, TO, L, RR),
    combine_list(L, RR, R).
combine_list([H|T], F, [H|F]).

set_element(L, I, X, R) :-
    get_rest_list(L, I, TL),
    II is I - 1,
    get_head_list(L, II, HL),
    append(HL, [X|[]], RR),
    append(RR, TL, R).
set_element([], _, X, [X|[]]).
set_element(L, 0, X, [X|L]).
set_element([H|T], 1, X, [X|T]).

抱歉!评论已关闭.