Joe Blow
Joe Blow

Reputation: 496

How to build a number from a list of numbers in Prolog?

I'm pretty new in Prolog, and I was trying (without any success) to get a number from it's representation as a list of numbers. Eg: L=[1,2,3] => N=123 I managed to build this recursive algorithm but it sais "Arithmetic Conv is not a function". Can someone help me correcting it?

    conv([],0).
    conv([H|T],R):-
         R is H*10+conv(T,R).
    conv([E],R):-
         R is E.

Upvotes: 1

Views: 409

Answers (1)

repeat
repeat

Reputation: 18726

Look at this answer to a recent related question!

It presents the pure Prolog predicate n_base10/2 using .

:- use_module(library(clpfd)).

Sample queries:

?- n_base10(123, [1,2,3]).
true.

?- n_base10(123, Ds).
Ds = [1,2,3].

?- n_base10(N, [1,7,9]).
  N = 179
; false.

?- n_base10(459183754813957135135239458256, Ds).
Ds = [4,5,9,1,8,3,7,5,4,8,1,3,9,5,7,1,3,5,1,3,5,2,3,9,4,5,8,2,5,6]. 

Upvotes: 1

Related Questions