質問

私はこのようなことをしています:

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

そのようなworkaroudのような気がしないような方法はありますか?

役に立ちましたか?

解決

atom_is_lower(N) :-
    atom_chars(N, [L]),
    char_type(L, lower).
  • atom_chars converts the atom into a list of characters.
  • char_type checks the type of a character.

Note that the second part (char_type) is necessary because a single-character atom can be a number (for example).

他のヒント

One option is to use the built-in char_type/2 and if it encounters a type error (e.g. when the input is longer than one symbol) and throws an exception then convert the exception to failure.

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

This solution can also generate lowercase letters:

?- atom_is_lower(A).
A = a ;
A = b ;
A = c ;
A = d ;
A = e ;
...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top