string_upper / 2的替代方案

时间:2018-04-29 00:24:40

标签: prolog swi-prolog

我的代码应该处理的机器由于某种原因不知道谓词string_upper/2。还有其他选择吗?最终,这个谓词的代码是什么样的?

2 个答案:

答案 0 :(得分:0)

将字符串分解为字符代码列表。然后在重建字符串之前,在必要时将这些代码转换为适当的大写代码。一个更完整的答案需要知道你正在使用哪个序言,它的可用谓词&它是如何表示字符串的。

您可能想要阅读不同的Prolog对字符,Unicode和字符串的处理。为此,SWI-Prolog实现有一些很好的文档:

传统(爱丁堡),ISO& SWI prolog处理字符:4.2 Character Representation

ISO& SWI prologs支持Unicode 2.16.1.8 Unicode Prolog source

SWI如何使用特定的字符串类型对象来使用引号来表示它们:5.2 The string type and its double quoted syntax

答案 1 :(得分:0)

你在评论中给出的例子实际上不是一个字符串而是一对原子。由于您已经选择使用upcase_atom/2,因此在您从帖子中删除的代码中,我指出这个内置只能在一个方向上工作,即如果第一个参数是原子的。请考虑以下查询:

?- upcase_atom(bo,'BO').   % Is 'BO' the uppercase version of bo?
true.                      % Yes, it is.

?- upcase_atom(bo,B).      % What's the uppercase version of bo?
B = 'BO'.                  % It's 'BO'

?- upcase_atom(B,'BO').    % What atom has 'BO' as uppercase version?
ERROR: upcase_atom/2: Arguments are not sufficiently instantiated

?- upcase_atom(B,1).       % What atom has 1 as uppercase version?
ERROR: upcase_atom/2: Arguments are not sufficiently instantiated

您的已删除代码中的示例查询为?- divideIt(a-b).。原子a与规则V开头的变量divideIt/1统一。然后Prolog调用必须失败的第一个目标upcase_atom(V,ve),因为原子ve不是原子a的大写版本。因此,您的示例查询也会失败。如果对谓词应用以下次要更改,则会产生所需的结果:

divideIt(V-W) :-
   upcase_atom(V,Ve),     % <- Ve (=variable) instead of ve (=atom)
   write(Ve),
   write('-'),            % <- '-' instead of "-"
   upcase_atom(W,We),     % <- We (=variable) instead of we (=atom)
   write(We).

?- divideIt(a-b).
A-B
true.