Проверьте, является ли атом строчной буквы

StackOverflow https://stackoverflow.com/questions/5496313

  •  14-11-2019
  •  | 
  •  

Вопрос

Я делаю это так:

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

Есть ли способ, которым не чувствует себя такой рабочей обработки?

Это было полезно?

Решение

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