prolog - Determine the type of characters -
i determine in prolog type of string of characters, if alphabetic, alphanumeric or numeric. example:
"i use page" alphabetic "0c0d24e" alphanumeric
how can do?
the predicate available char_type/2, or better, code_type/2. apply each code in string, use maplist/2. problem it's wrong arguments order of code_type. service predicate needed (or download lambda, if you're using swi-prolog, ?- pack_install(lambda).
).
without lambda:
code_type_(x,y) :- code_type(y,x). ?- maplist(code_type_(alpha), "abc"). true.
with lambda:
?- [library(lambda)]. ?- maplist(\c^code_type(c,alpha), "abc"). true.
edit after comments, it's apparent more flexible parsing required. dcg it's recommended way go: library(dcg/basics) offers prebuilt 'categorizer', , highlights proper way write own, combining code_type: instance, here added rule:
%% prolog_var_name(-name:atom)// semidet. % % matches prolog variable name. intended deal % quasi quotations embed prolog variables. prolog_var_name(name) --> [c0], { code_type(c0, prolog_var_start) }, !, prolog_id_cont(cl), { atom_codes(name, [c0|cl]) }. prolog_id_cont([h|t]) --> [h], { code_type(h, prolog_identifier_continue) }, !, prolog_id_cont(t). prolog_id_cont([]) --> "".
see how code_type/2 used qualify single characters...
more edit - note: untested
qualify_atom(atom, type) :- atom_codes(atom, codes), qualify_codes(codes, type). qualify_codes(codes, type) :- ( maplist(code_type_(alnum), codes) -> type = alnum ; maplist(code_type_(alpha), codes) -> type = alpha ; type = unknown ).
then, work on list
?- maplist(qualify_atom, atoms, types).
edit
an update of answer mandatory: since library(yall) has been released in swi-prolog, , autoloaded, can write:
?- maplist([c]>>code_type(c,alpha), `abc`).
also, note change in literal representation: double quotes in swi-prolog ver.7+ don't represent anymore list of character codes.
Comments
Post a Comment