How to convert a string read from input to a list of lists in prolog -


i'm trying write program converts mayan arabic numerals , vice versa in prolog. although i'm still running trouble i've managed working. i'm wondering how if read example:

....| 0 .| ||| 

from user input, how can convert list this:

l = [ ['.','.','.','.','|'], [0], ['.','|'], ['|','|','|'] ] 

i have algorithm written getting l arabic value, have no clue how convert string list.

take @ this answer. code i've written there splits prolog string on spaces generate list of atoms. small modification, can alter code create strings instead of atoms. here relevant code previous post, necessary modification case:

data([a|as]) -->      spaces(_),      chars([x|xs]),      {string_to_list(a, [x|xs])},  %% using string_to_list/2 instead     spaces(_),      data(as). data([]) --> [].  chars([x|xs]) --> char(x), !, chars(xs). chars([]) --> [].  spaces([x|xs]) --> space(x), !, spaces(xs). spaces([]) --> [].  space(x) --> [x], {code_type(x, space)}.  char(x) --> [x], {\+ code_type(x, space)}. 

in example, you'd take prolog string containing example "....| 0 .| |||" , run above code using built-in phrase/2, so:

?- phrase(data(numerallist), "....| 0 .| |||"). numerallist = ["....|", "0", ".|", "|||"] 

note i've tested on swi-prolog , works, if you're using different prolog implementation, mightn't support dcgs or built-ins i've used.

if after result precisely you've described l above, can modify code further return list [x|xs] directly in data clause (removing sub-goal {string_to_list(a, [x|xs])},), , change last predicate char following:

char(c) --> [x], {\+ code_type(x, space), atom_codes(c,[x])}. 

running gives:

?- phrase(data(l), "....| 0 .| |||"). l = [['.', '.', '.', '.', '|'], ['0'], ['.', '|'], ['|', '|', '|']] 

edit: requested, here modified code generates above result in full:

data([[x|xs]|as]) -->      spaces(_),      chars([x|xs]),      spaces(_),      data(as). data([]) --> [].  chars([x|xs]) --> char(x), !, chars(xs). chars([]) --> [].  spaces([x|xs]) --> space(x), !, spaces(xs). spaces([]) --> [].  space(x) --> [x], {code_type(x, space)}.  char(c) --> [x], {\+ code_type(x, space), atom_codes(c,[x])}. 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -