String Manipulation

Jun 7, 2010 at 9:14 PM


Most string manipulation methods seem to fail in the PrologWorkbench. The only one that appears to be working is "contains". None of the others are predicates so I can see why they don't work. Is this intended behavior? If so how should I work with strings?


for example the following always fails

:- substring("abc",1).


this one always suceeds

:- length("abc").

Thank you

Dec 11, 2011 at 7:12 PM

This actually is working.  "substring" is a expression that returns a string,  Prolog then attempts to convert that to boolean to determine success or failure.  The result in the example above is "a" which cannot be converted to a boolean - hence the failure. length("abc") returns 3 which is convertible to a boolean True.  On the other hand, the expression length("") returns 0 so it would also fail since 0 converts to a boolean False.

Using the "is" operator (or the equivalent ":=") allows you to evaluate substring or the other string functions.  For example:

:- X := substring("123",1)

binds X to "23".