I've been having trouble trying to split numbers into lists using Prolog, e.g. 123456 becomes [1,2,3,4,5,6]
.
Can you please help me work out how to do this?
I've been having trouble trying to split numbers into lists using Prolog, e.g. 123456 becomes [1,2,3,4,5,6]
.
Can you please help me work out how to do this?
You could first create a reverse list:
The recursive step works like this:
divides N by 10 and rounds it down. So 538 becomes 53.8 becomes 53. It cuts off the last digit.
takes the remainder of N divided by 10. 538 mod 10 equals 8. So you get only the last digit.
Now for splitting the list you only need to reverse the list created by splitRev/2. So predicate split/2 is defined as:
Note that reverse/2 is a built-in predicate.
I hope this helps!
the builtins available are ISO standard:
I also have some very old code I developed for my interpreter.
:=
must be renamedis
to run with standard Prologs. But then you are best served from above builtins...edit here the additional call required to get numbers: