How to give values to items in a list in prolog

故事扮演 提交于 2020-01-16 14:09:50

问题


So i made a list using atom_chars(X,Y). Which split the string 'abc' into [a,b,c]. I now want to assign numbers to the elements in the list. Such as a is 4, b is 2, c is 7.

How would i go about doing this?


回答1:


In Prolog, the symbols a, b, and c are considered atoms. You can't "assign" values to them. You could, however, associate numbers with them using, for example, - as a convenient notation for a term. You could form a list:

[a-2, b-4, c-3]

Let's say you bind this to the variable AssocList. Then if you have a letter or character bound to C, you can query:

member(C-N, AssocList)

This will bind N to the number associated with C. Likewise, if you have a number, it will yield all of the characters C that are associated with that number.



来源:https://stackoverflow.com/questions/59120632/how-to-give-values-to-items-in-a-list-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!