What is the best way to convert binary bits (it might be a list of 0/1, for example) into numbers in a reversible way. I've written a native predicate in swi, but is there better solution ? Best regards
相关问题
- Creating a SPARQL parameterized query using append
- How to join rules and print out outputs in prolog
- Splitting list and iterating in prolog
- Accumulating while in recursion/backtracking
- prolog trace how to use
相关文章
- What are the problems associated to Best First Sea
- How can I fix this circular predicate in Prolog?
- How to negate in Prolog
- Remove incorrect subsequent solutions without once
- prolog two lists are exactly the same
- Simplify Expressions in Prolog
- Check if any element's frequency is above a li
- Prolog — symetrical predicates
playing with bits...
The solution
This answer seeks to provide a predicate
binary_number/2
that presents both logical-purity and the best termination properties. I've usedwhen/2
in order to stop queries likecanonical_binary_number(B, 10)
from going into infinite looping after finding the first (unique) solution. There is a trade-off, of course, the program has redundant goals now.Purity and termination
I claim that this predicate presents logical-purity from construction. I hope I got it right from these answers: one, two and three.
Any goal with proper arguments terminates. If arguments need to be checked, the simplest way to achieve this is using the built-in
length/2
:Example queries
Here is the solution I was thinking of, or rather what I hoped exists.
This solution also terminates for queries like:
Use CLP(FD) constraints, for example:
Example queries: