1

我这样做是这样的:

foo(N) :-
        name(N, [Code]),
        name(a, [CodeA]),
        name(z, [CodeZ]),
        CodeA =< Code,
        Code  =< CodeZ.

有没有一种方法感觉不像是一种工作方式?

4

2 回答 2

3
atom_is_lower(N) :-
    atom_chars(N, [L]),
    char_type(L, lower).

请注意,第二部分 (char_type) 是必需的,因为单字符原子可以是数字(例如)。

于 2011-03-31T07:16:38.040 回答
0

一种选择是使用内置函数char_type/2,如果它遇到类型错误(例如,当输入长于一个符号时)并抛出异常,则将异常转换为失败。

atom_is_lower(Atom) :-
    catch(char_type(Atom, lower), _, fail).

此解决方案还可以生成小写字母:

?- atom_is_lower(A).
A = a ;
A = b ;
A = c ;
A = d ;
A = e ;
...
于 2011-03-31T11:02:04.197 回答