二进制到十进制 - 序言 [英] Binary to decimal - prolog

查看:131
本文介绍了二进制到十进制 - 序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个堆栈:<一href=\"http://stackoverflow.com/questions/4192063/reversible-binary-to-number-$p$pdicate/27788081#27788081\">reversible &QUOT;二进制数&QUOT; predicate

但我不明白

:- use_module(library(clpfd)).

binary_number(Bs0, N) :-
        reverse(Bs0, Bs),
        binary_number(Bs, 0, 0, N).

binary_number([], _, N, N).
binary_number([B|Bs], I0, N0, N) :-
        B in 0..1,
        N1 #= N0 + (2^I0)*B,
        I1 #= I0 + 1,
        binary_number(Bs, I1, N1, N).

查询示例:

?- binary_number([1,0,1], N).
N = 5.

?- binary_number(Bs, 5).
Bs = [1, 0, 1] .

有人能解释我的code

Could somebody explain me the code

此特别是具有: binary_number([],_,N,N)(该_)

Especialy this : binary_number([], _, N, N). (The _ )

还有什么呢库(clpfd)呢?

Also what does library(clpfd) do ?

为什么反向(BS0,BS)?我把它拿走它仍然能正常工作...

And why reverse(Bs0, Bs) ? I took it away it still works fine...

THX提前

推荐答案

在原来的 binary_number([],_,N,N)。 _ 意味着你不在乎什么变量的值。如果你使用 binary_number([],X,N,N)。(不关心什么<​​code> X 为)序言会发出一个单变量警告。此外,这是什么predicate条款说的是,当第一个参数是 [] (空单),然后是第三和第四参数是统一的。

In the original, binary_number([], _, N, N)., the _ means you don't care what the value of the variable is. If you used, binary_number([], X, N, N). (not caring what X is), Prolog would issue a singleton variable warning. Also, what this predicate clause says is that when the first argument is [] (the empty list), then the 3rd and 4th arguments are unified.

由于在评论中解释, use_module(库(clpfd))导致Prolog的使用库的约束逻辑程序设计了有限域的。您也可以通过谷歌搜索序言clpfd。

As explained in the comments, use_module(library(clpfd)) causes Prolog to use the library for Constraint Logic Programming over Finite Domains. You can also find lots of good info on it via Google search of "prolog clpfd".

通常,在Prolog中,算术前$ P $比较pssions要求除权pressions中完全实例:

Normally, in Prolog, arithmetic expressions of comparison require that the expressions be fully instantiated:

X + Y =:= Z + 2.  % Requires X, Y, and Z to be instantiated

序言将评估并做比较,产生真或假。如果这些变量并没有实例化,它会抛出一个错误。同样地,用于分配,则是/ 2 predicate要求右侧前pression与特定变量充分评价的所有实例

Prolog would evaluate and do the comparison and yield true or false. It would throw an error if any of these variables were not instantiated. Likewise, for assignment, the is/2 predicate requires that the right hand side expression be fully evaluable with specific variables all instantiated:

Z is X + Y.  % Requires X and Y to be instantiated

使用CLPFD你可以有你的Prolog探讨的解决方案。而且你还可以指定你想要的变量限制到什么领域。所以,你可以说 X + Y#= Z + 2 和前导可以枚举 X有可能的解决方案以Z

顺便说一句,原来实行可重构一点,以避免幂每次和消除逆转

As an aside, the original implementation could be refactored a little to avoid the exponentiation each time and to eliminate the reverse:

:- use_module(library(clpfd)).

binary_number(Bin, N) :-
    binary_number(Bin, 0, N).

binary_number([], N, N).
binary_number([Bit|Bits], Acc, N) :-
    Bit in 0..1,
    Acc1 #= Acc*2 + Bit,
    binary_number(Bits, Acc1, N).

这非常适用于查询,如:

This works well for queries such as:

| ?- binary_number([1,0,1,0], N).

N = 10 ? ;

no
| ?- binary_number(B, 10).

B = [1,0,1,0] ? ;
B = [0,1,0,1,0] ? ;
B = [0,0,1,0,1,0] ? ;
...

但它有终止的问题,如在评论中指出,对于这样的情况下, BS = [1 | _],N#=&LT; 5,binary_number(BS,N)。 A 溶液$ P $由@false psented它只是修改了上述有助于解决这些问题的终止。我在这里重申,解决方案方便:

But it has termination issues, as pointed out in the comments, for cases such as, Bs = [1|_], N #=< 5, binary_number(Bs, N). A solution was presented by @false which simply modifies the above helps solve those termination issues. I'll reiterate that solution here for convenience:

:- use_module(library(clpfd)).

binary_number(Bits, N) :-
    binary_number_min(Bits, 0,N, N).

binary_number_min([], N,N, _M).
binary_number_min([Bit|Bits], N0,N, M) :-
    Bit in 0..1,
    N1 #= N0*2 + Bit,
    M #>= N1,
    binary_number_min(Bits, N1,N, M).

这篇关于二进制到十进制 - 序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆