Did you know ... | Search Documentation: |
Pack onepointfour_basics -- prolog/doc/README_stringy_length.md |
stringy_length.pl
stringy_length.plt
](../stringy_length.plt) (0BSD license)
Determine length of an atom or an SWI-Prolog string (a "stringy thing").
This is a mashup of Prolog's ISO-standard
atom_length/2
and the additional SWI-Prolog
string_length/2.
Because having several X_length/2
to handle "stringy things" just
doesn't make much sense in an anytyped language. Note that the
implementation of SWI-Prolog's atom_length/2 and string_length/2
accepts atoms and strings equally.Please refer to the README.md file, but in short:
?- assertz(file_search_path(library,'/foo/bar/prolog_code/unpacked')). ?- use_module(library('onepointfour_basics/stringy_length.pl')). ?- load_test_files([]). ?- run_tests.
hard
or soft
to
control behaviour for negative Length and bad Type.
If Tuned = hard
the predicates throw instead of failing if:
atom
or string
.Determining length of a string:
?- stringy_length("Hello",L). L = 5.
Determining length of an atom:
?- stringy_length('Hello',5). true.
Verifying length of a string:
?- stringy_length("Hello",100). false.
What does it say anout negative length?
?- stringy_length("Hello",-1). false.
The above is a reasonably "logical" response, but you may want more discipline:
?- stringy_length("Hello",-1,hard). ERROR: check failed : 'domain' error (the culprit is outside the required domain) ERROR: message : the value should fulfill "integer that is >= 0"-ness ERROR: culprit : -1
Additionally finding the type:
?- stringy_length_type(hello,Length,Type). Length = 5, Type = atom.
or accepting the type:
?- stringy_length_type(hello,Length,atom). Length = 5.
or not as the case may be:
?- stringy_length_type(hello,Length,string). false.
Using hard
mode is always recommended:
?- stringy_length_type(hello,Length,gark,hard). ERROR: check failed : 'domain' error (the culprit is outside the required domain) ERROR: message : the value should fulfill "stringy_typeid"-ness ERROR: culprit : gark