prolog - reversible "binary to number" predicate -


what best way convert binary bits (it might list of 0/1, example) numbers in reversible way. i've written native predicate in swi, there better solution ? best regards

use clp(fd) constraints, example:

:- use_module(library(clpfd)).  binary_number(bs0, n) :-         reverse(bs0, bs),         foldl(binary_number_, bs, 0-0, _-n).  binary_number_(b, i0-n0, i-n) :-         b in 0..1,         n #= n0 + b*2^i0,         #= i0 + 1. 

example queries:

?- binary_number([1,0,1], n). n = 5.  ?- binary_number(bs, 5). bs = [1, 0, 1] .  ?- binary_number(bs, n). bs = [], n = 0 ; bs = [n], n in 0..1 ; etc. 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -