How to find the length of a string using prolog

I have a method length (list, var) that gives me the length of a list, but I want to find the length of a string, anyone with a solution?

+3
source share
4 answers

just played with him and I gave a predicate how

?- length("hello", R).
R = 5.

unexpectedly it worked :)

+1
source

If you want to go through the ISO Prolog, then you will not use the predicate name/2.

ISO Prolog offers you atom_codes/2and atom_chars/2. They offer you the functionality of converting the atom back to a list of codes or a list of characters. Atoms are Prolog system strings and symbols are just atoms of length 1. Here are some examples of invokations from two predicates:

?- atom_codes(ant, L).
L = [97,110,116]
?- atom_codes(X, [97,110,116]).
X = ant
?- atom_chars(ant, X).
X = [a,n,t]
?- atom_chars(X,[a,n,t]).
X = ant

, aka atom. , . ISO atom_length/2 . :

?- atom_length(ant,X).
X = 3

Prolog , SWI Prolog, GNU Prolog, Jekejeke Prolog .. Prolog. 8- , 16- 32- .

, - , , . , .

Bye

+6

- , , , .. atom_chars/2:

atom_chars(StrVar, ListVar), length(ListVar, LengthVar).
+3

Perhaps it:

string_length(+String, -Length)

from the Prolog manual.

0
source

All Articles