I’m learning Prolog and I need idea how to convert a list of strings:
['f(a,45)', 'f(b,13)', 'f(c,12)']
into a list of pairs that looks like this:
[[45,'a'],[13,'b'],[12,'c']]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s a list of atoms, not strings. Strings in Prolog are usually list of character codes, expressed like
Anyway, apply a conversion to each element with a recursive function:
Instead of recursion you could use maplist/3 in this way:
To convert an element, you need a parser. DCGs are convenient:
test:
atomc//1 & numc//1 are expressed in a compact way, but are very simple recursive pattern matching procedures, i.e. atomc//1 could be