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.
In SWI-Prolog there is a builtin that almost gives what you need:
Splitting on spaces can be done with another builtin, used in 'reverse' mode:
Then we can combine these using maplist:
G it's very similar to what you need, just handle the '0'...
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
L
above, you can modify the code further to return the list [X|Xs] directly in thedata
clause (removing the sub-goal{string_to_list(A, [X|Xs])},
), and change the last predicatechar
to the following:Running this gives:
EDIT: As requested, here is the modified code which generates the above result in full: