将数字转换为 Prolog 中的列表?
Convert numbers to list in Prolog?
我创建了一个随机二进制生成器,但它的“输出”是书面数字,我如何才能将它们制成列表?
bin_gen(0)。
bin_gen(Y) :- random(0,2,S),write(S),Y1 是 Y - 1,bin_gen(Y1).
Y是长度。例如,我希望在 bin_gen(0).
之后输出 1011 为 [1,0,1,1]
十进制转二进制:
转换步骤:
Divide the number by 2.
Get the integer quotient for the next iteration.
Get the remainder for the binary digit.
Repeat the steps until the quotient is equal to 0.
序言代码:
decimal_binary(Dec,Binary):-
( Dec=0 -> %If Decimal is 0 then return Binary 0
write('Binary = [0]'); %Else
decimal_binary1(Dec,Binary1), % Get Binary List
reverse(Binary1,Binary),!). %Reverse the Binary List
decimal_binary1(0,[]). %Base Case: Stop when Quotient is 0.
decimal_binary1(Dec,[Remainder|List]):- %Generate Binary List till Base Case succeeds
divmod(Dec,2,Quotient,Remainder), %Built-in to get Quotient and Remainder
decimal_binary1(Quotient,List).
divmod(Dividend, Divisor, Quotient, Remainder) :- %Built-in to get Quotient and Remainder
Quotient is Dividend div Divisor,
Remainder is Dividend mod Divisor.
示例:
?- decimal_binary(13,Binary)
Binary = [1, 1, 0, 1]
?- decimal_binary(126,Binary)
Binary = [1, 1, 1, 1, 1, 1, 0]
?- decimal_binary(75,Binary)
Binary = [1, 0, 0, 1, 0, 1, 1]
?- decimal_binary(0,Binary)
Binary = [0]
给定一个整数,您可以将其反复除以 10,将余数作为个位数字,并将向下舍入的结果用于下一次迭代,从而将其转换为数字列表:
list_digits(Int, Digits) :-
list_digits_aux(Int, [], Digits).
list_digits_aux(Int, Digits, [Int|Digits]) :- Int < 10.
list_digits_aux(Int, Digits, Acc) :-
NextInt is Int div 10,
NextInt > 0,
D is Int rem 10,
list_digits_aux(NextInt, [D|Digits], Acc).
在这段代码中,我们使用了带有累加器参数的辅助谓词,这样我们就不必在最后反转结果。
但是,iiuc,如果你想要数字列表,你可以稍微调整你当前的谓词来构建数字列表,而不是打印出每个数字:
bin_gen(0, []).
bin_gen(Y, [D|Digits]) :-
random(0,2,D),
succ(Y1, Y),
bin_gen(Y1, Digits).
我创建了一个随机二进制生成器,但它的“输出”是书面数字,我如何才能将它们制成列表?
bin_gen(0)。 bin_gen(Y) :- random(0,2,S),write(S),Y1 是 Y - 1,bin_gen(Y1).
Y是长度。例如,我希望在 bin_gen(0).
之后输出 1011 为 [1,0,1,1]十进制转二进制:
转换步骤:
Divide the number by 2.
Get the integer quotient for the next iteration.
Get the remainder for the binary digit.
Repeat the steps until the quotient is equal to 0.
序言代码:
decimal_binary(Dec,Binary):-
( Dec=0 -> %If Decimal is 0 then return Binary 0
write('Binary = [0]'); %Else
decimal_binary1(Dec,Binary1), % Get Binary List
reverse(Binary1,Binary),!). %Reverse the Binary List
decimal_binary1(0,[]). %Base Case: Stop when Quotient is 0.
decimal_binary1(Dec,[Remainder|List]):- %Generate Binary List till Base Case succeeds
divmod(Dec,2,Quotient,Remainder), %Built-in to get Quotient and Remainder
decimal_binary1(Quotient,List).
divmod(Dividend, Divisor, Quotient, Remainder) :- %Built-in to get Quotient and Remainder
Quotient is Dividend div Divisor,
Remainder is Dividend mod Divisor.
示例:
?- decimal_binary(13,Binary)
Binary = [1, 1, 0, 1]
?- decimal_binary(126,Binary)
Binary = [1, 1, 1, 1, 1, 1, 0]
?- decimal_binary(75,Binary)
Binary = [1, 0, 0, 1, 0, 1, 1]
?- decimal_binary(0,Binary)
Binary = [0]
给定一个整数,您可以将其反复除以 10,将余数作为个位数字,并将向下舍入的结果用于下一次迭代,从而将其转换为数字列表:
list_digits(Int, Digits) :-
list_digits_aux(Int, [], Digits).
list_digits_aux(Int, Digits, [Int|Digits]) :- Int < 10.
list_digits_aux(Int, Digits, Acc) :-
NextInt is Int div 10,
NextInt > 0,
D is Int rem 10,
list_digits_aux(NextInt, [D|Digits], Acc).
在这段代码中,我们使用了带有累加器参数的辅助谓词,这样我们就不必在最后反转结果。
但是,iiuc,如果你想要数字列表,你可以稍微调整你当前的谓词来构建数字列表,而不是打印出每个数字:
bin_gen(0, []).
bin_gen(Y, [D|Digits]) :-
random(0,2,D),
succ(Y1, Y),
bin_gen(Y1, Digits).