|Did you know ...||Search Documentation:|
) or a compound term with name‘
’125The traditional list functor name is the dot (
’.'). This is still the case of the command line option --traditional is given. See also section 5.1. and arity 2 and the second argument is a list.126In versions before 5.0.1, is_list/1 just checked for
[_|_]and proper_list/1 had the role of the current is_list/1. The current definition conforms to the de facto standard. Assuming proper coding standards, there should only be very few cases where a quick-and-dirty is_list/1 is a good choice. Richard O'Keefe pointed at this issue. This predicate acts as if defined by the definition below on acyclic terms. The implementation safely fails if Term represents a cyclic list.
is_list(X) :- var(X), !, fail. is_list(). is_list([_|T]) :- is_list(T).
This shows how to convert almost any "string" of 0s and 1s to an integer.
It takes advantage of the built-in term reading, thus avoiding an explicit loop and arithmetic.
binary_string_to_int(Str, Int) :- ( ( string(Str) ; atom(Str) ) -> atomics_to_string(["0b", Str], Literal) ; is_list(Str) -> atomics_to_string(["0b"|Str], Literal) ; type_error(string_type, Str) ), catch(term_string(Int, Literal, ), error(syntax_error(_), _), type_error(string_of_0s_and_1s, Str)).
?- binary_string_to_int('101', X). X = 5. ?- binary_string_to_int([0, 0, 1, 0, 1], X). X = 5. ?- binary_string_to_int(["1", "1", "0"], X). X = 6.
The library(lists) contains a number of old predicates for manipulating sets represented as unordered lists, notably intersection/3, union/3, subset/2 and subtract/3. These predicates all use memberchk/2 to find equivalent elements. As a result these are not logical while unification can easily lead to dubious results. Operating on unordered sets these predicates typically have complexity |Set1|*|Set2|.
When using these predicates
?- intersection([a,b,c], [b,e], X). X = [b]. ?- union([a,b,c], [b,e], X). X = [a, c, b, e]. ?- subset([a,b,c], [b,e]). false. ?- subset([b], [b,e]). true. ?- subtract([a,b,c], [b,e], X). X = [a, c].
?- intersection([a,b,c], [b,E], X). E = a, X = [a, b]. ?- subtract([a,b,c], [b,E], X). E = a, X = [c].
Insufficient instantiation also leads to problems
?- subtract([a,b,c], X, [c]). false.
Note that duplicates in the input may result in duplicates in the output (example by Boris).
?- intersection([a,b,a], [b,a,b], I). I = [a, b, a].