Normalization of gaps in Prolog atoms

What is the best way to normalize whitespace (space, new line, tab) in the Prolog atom, for example. in SWI-Prolog. That is, I would like to have a rule:

normalize_space_in_atom(+Atom1, -Atom2)

such that Atom2

  • has any sequence of space characters turned into one space
  • starts with a non-space
  • ends with a non-space
+3
source share
1 answer

SWI Prolog provides normalize_space / 2, so you can define your predicate as follows:

normalize_space_in_atom(A1,A2) :- normalize_space(atom(A2),A1).

I tried this with SWI Prolog 5.7.5 and it seems to work. You can add additional error handling if you want.

+2
source

Source: https://habr.com/ru/post/1702658/


All Articles