I’m trying to write a program that converts from Mayan to Arabic numerals and vice versa in prolog. Although I’m still running into some trouble I’ve managed to get it mostly working. I’m just wondering how if I read for example:
....| 0 .| |||
from a user input, how can I convert it to a list like this:
L = [ ['.','.','.','.','|'], [0], ['.','|'], ['|','|','|'] ]
I have an algorithm written getting from L to the arabic value, but I have no clue how to convert that string into a list.
Take a look at this answer. The code I’ve written there splits a Prolog string on spaces to generate a list of atoms. With a small modification, you can alter the code to create strings instead of atoms. Here is the relevant code from the previous post, with the necessary modification for your case:
In your example, you’d take a Prolog string containing your example like
"....| 0 .| |||"and run the above code using the built-inphrase/2, like so:Note that I’ve tested this on SWI-Prolog and it works, but if you’re using a different Prolog implementation, it mightn’t support DCGs or the built-ins I’ve used.
If you were after a result which is precisely like what you’ve described as
Labove, you can modify the code further to return the list [X|Xs] directly in thedataclause (removing the sub-goal{string_to_list(A, [X|Xs])},), and change the last predicatecharto the following:Running this gives:
EDIT: As requested, here is the modified code which generates the above result in full: