prolog:用第一个替换列表中的第n个项目

发布于 2025-01-22 02:19:38 字数 203 浏览 0 评论 0原文

我想拥有一个可以用第一个替换列表中的第n个项目的序言。

示例:

% replace(+List,+Counter,-New List, %-First Item).
?- replace([1,2,3,4,5],3,L,Z).

L = [1, 2, 1, 4, 5]
Z = 1

我不知道该怎么做。感谢您的帮助!

I'd like to have a Prolog predicate that can replace the nth item in the list with the first.

Example:

% replace(+List,+Counter,-New List, %-First Item).
?- replace([1,2,3,4,5],3,L,Z).

L = [1, 2, 1, 4, 5]
Z = 1

I don't know how to do this. Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残疾 2025-01-29 02:19:38

尝试使用谓词nth1(索引,列表,项目,休息)

?- nth1(3, [1,2,3,4,5], Item, Rest).
Item = 3,
Rest = [1, 2, 4, 5].

?- nth1(3, List, 1, [1,2,4,5]).
List = [1, 2, 1, 4, 5].

将其放在一起:

replace(List, Index, NewList, First) :-
    List = [First|_],
    nth1(Index, List, _Removed, Rest),
    nth1(Index, NewList, First, Rest).

示例:

?- replace([1,2,3,4,5], 3, L, Z).
L = [1, 2, 1, 4, 5],
Z = 1.

?- replace([one,two,three,four,five], 4, NewList, First).
NewList = [one, two, three, one, five],
First = one.

Try using the predicate nth1(Index, List, Item, Rest):

?- nth1(3, [1,2,3,4,5], Item, Rest).
Item = 3,
Rest = [1, 2, 4, 5].

?- nth1(3, List, 1, [1,2,4,5]).
List = [1, 2, 1, 4, 5].

Putting it all together:

replace(List, Index, NewList, First) :-
    List = [First|_],
    nth1(Index, List, _Removed, Rest),
    nth1(Index, NewList, First, Rest).

Examples:

?- replace([1,2,3,4,5], 3, L, Z).
L = [1, 2, 1, 4, 5],
Z = 1.

?- replace([one,two,three,four,five], 4, NewList, First).
NewList = [one, two, three, one, five],
First = one.
稚然 2025-01-29 02:19:38

另一种方法,稍快(用简单的算术替换为succ/2有助于性能):

replace_nth_with_first(Nth1, Lst, LstReplaced, First) :-
    must_be(positive_integer, Nth1),
    Lst = [First|_Tail],
    replace_nth_with_first_(Nth1, Lst, LstReplaced, First).

% Keeping the arguments simple, to ensure that Nth1 = 1 matches
replace_nth_with_first_(1, Lst, LstReplaced, First) :-
    !,
    Lst = [_Head|Tail],
    LstReplaced = [First|Tail].
replace_nth_with_first_(Nth1, [H|Lst], [H|LstReplaced], First) :-
    Nth is Nth1 - 1,
    replace_nth_with_first_(Nth, Lst, LstReplaced, First).

结果:swi-prolog:

?- replace_nth_with_first(3, [a, b, c, d, e], R, F).
R = [a,b,a,d,e],
F = a.

绩效比较:

?- numlist(1, 2000000, L), time(replace_nth_with_first(1000000, L, R, First)).
% 1,000,004 inferences, 0.087 CPU in 0.087 seconds (100% CPU, 11428922 Lips)

% slago's
?- numlist(1, 2000000, L), time(replace_f(L, 1000000, R, First)).
% 2,000,011 inferences, 0.174 CPU in 0.174 seconds (100% CPU, 11484078 Lips)

请注意参数顺序,按照 https://swi-prolog.discourse.group/t/split-list/4836/8

Another method, slightly faster (replacing succ/2 with simple arithmetic helped performance):

replace_nth_with_first(Nth1, Lst, LstReplaced, First) :-
    must_be(positive_integer, Nth1),
    Lst = [First|_Tail],
    replace_nth_with_first_(Nth1, Lst, LstReplaced, First).

% Keeping the arguments simple, to ensure that Nth1 = 1 matches
replace_nth_with_first_(1, Lst, LstReplaced, First) :-
    !,
    Lst = [_Head|Tail],
    LstReplaced = [First|Tail].
replace_nth_with_first_(Nth1, [H|Lst], [H|LstReplaced], First) :-
    Nth is Nth1 - 1,
    replace_nth_with_first_(Nth, Lst, LstReplaced, First).

Result in swi-prolog:

?- replace_nth_with_first(3, [a, b, c, d, e], R, F).
R = [a,b,a,d,e],
F = a.

Performance comparison:

?- numlist(1, 2000000, L), time(replace_nth_with_first(1000000, L, R, First)).
% 1,000,004 inferences, 0.087 CPU in 0.087 seconds (100% CPU, 11428922 Lips)

% slago's
?- numlist(1, 2000000, L), time(replace_f(L, 1000000, R, First)).
% 2,000,011 inferences, 0.174 CPU in 0.174 seconds (100% CPU, 11484078 Lips)

Note the argument order, as per https://swi-prolog.discourse.group/t/split-list/4836/8

负佳期 2025-01-29 02:19:38

您可以使用附加/3。尼斯,简洁和宣言:

replace( [X|Xs] , 1 , [X|Xs], X ) .  % replacing the first item in the list is a no-op
replace( Ls     , I , L1    , X ) :- % for anything else...
  I > 1 ,                            % - the index must be greater than 1
  J is I-1 ,                         % - the length of the prefix is I-1
  length( [X|Xs], J ) ,              % - construct an empty/unbound prefix list of the desired length
  append( [X|Xs] , [_|Ys] , Ls ) ,   % - break the source list up into the desired bits
  append( [X|Xs] , [X|Ys] , L1 )     % - and then put everything back together in the desired manner
  .                                  % Easy!

You might use append/3. Nice, concise, and declarative:

replace( [X|Xs] , 1 , [X|Xs], X ) .  % replacing the first item in the list is a no-op
replace( Ls     , I , L1    , X ) :- % for anything else...
  I > 1 ,                            % - the index must be greater than 1
  J is I-1 ,                         % - the length of the prefix is I-1
  length( [X|Xs], J ) ,              % - construct an empty/unbound prefix list of the desired length
  append( [X|Xs] , [_|Ys] , Ls ) ,   % - break the source list up into the desired bits
  append( [X|Xs] , [X|Ys] , L1 )     % - and then put everything back together in the desired manner
  .                                  % Easy!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文