问题
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