小编mag*_*gus的帖子

使用手动列表迭代与通过失败递归的优缺点是什么

我一直反对这一点,我无法确定攻击它的方法.以下是处理一些季节事实的两种方法.

我想弄清楚的是,是否使用方法1或方法2,以及每种方法的利弊是什么,特别是大量的事实.

methodone因为事实是可用的,所以似乎很浪费,为什么还要建立一个列表(特别是一个大的列表).如果列表足够大,这也必须有内存含义吗?它没有利用Prolog的自然回溯功能.

methodtwo利用回溯来为我做递归,我猜想会有更多的内存效率,但是通常这样做是不是很好的编程习惯呢?这可以说是更难以理解,可能还有其他副作用吗?

我可以看到的一个问题是每次fail调用时,我们都失去了将任何东西传递回调用谓词的能力,例如.如果是的话methodtwo(SeasonResults),因为我们故意不断地破坏谓词.所以methodtwo需要断言事实来存储状态.

大概(?)方法2会更快,因为它没有(大)列表处理吗?

我可以想象,如果我有一个清单,那么methodone将是要走的路......还是总是如此?在任何情况下都可以将列表声明为事实,methodone然后使用方法二处理它们?完全疯了吗?

但话说回来,我读到断言事实是一项非常"昂贵"的业务,所以列表处理可能是要走的路,即使是大型列表?

有什么想法吗?或者有时候使用一个而不是另一个更好,这取决于(什么)情况?例如.对于内存优化,使用方法2,包括断言事实,以及速度使用方法1?

season(spring).
season(summer).
season(autumn).
season(winter).

 % Season handling
showseason(Season) :-
    atom_length(Season, LenSeason),
    write('Season Length is '), write(LenSeason), nl.

% -------------------------------------------------------------
% Method 1 - Findall facts/iterate through the list and process each
%--------------------------------------------------------------
% Iterate manually through a season list
lenseason([]).
lenseason([Season|MoreSeasons]) :-
    showseason(Season),
    lenseason(MoreSeasons).


% Findall to build a list then iterate until all done
methodone :-
    findall(Season, season(Season), …
Run Code Online (Sandbox Code Playgroud)

prolog prolog-dif prolog-toplevel

8
推荐指数
2
解决办法
2032
查看次数

如何使用 SWI-Prolog 控制台中的向上/向下历史键?

当我按下向上/向下键时,我应该得到 - 就像 unix 一样 - 之前的命令,但我得到的是:

Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.0.0)
Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- ['nc'].
% nc compiled into nc 0.00 sec, 84 clauses
true.

2 ?- listing.
true.
Run Code Online (Sandbox Code Playgroud)

(我按“向上”箭头键返回“列表”命令……然后……)

3 ?- **^[[A**
Run Code Online (Sandbox Code Playgroud)

我从源代码编译,没有任何额外的“配置”或“制作”参数。

在以前版本的 swi-prolog …

bash ubuntu keyboard-shortcuts swi-prolog

5
推荐指数
1
解决办法
1336
查看次数