如何在序言中为列表中的项目赋值 [英] How to give values to items in a list in prolog

查看:23
本文介绍了如何在序言中为列表中的项目赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 atom_chars(X,Y) 制作了一个列表. 将字符串 'abc' 拆分为 [a,b,c]代码>.我现在想为列表中的元素分配数字.如a为4,b为2,c为7.

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.

我该怎么做?

推荐答案

在 Prolog 中,符号 abc 是考虑原子.您不能为它们分配"值.但是,您可以使用例如 - 作为术语的方便表示法将数字与它们相关联.你可以形成一个列表:

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]

假设您将其绑定到变量 AssocList.那么如果你有一个字母或字符绑定到C,你可以查询:

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)

这会将 N 绑定到与 C 关联的数字.同样,如果您有一个数字,它将产生与该数字相关联的所有字符 C.

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.

这篇关于如何在序言中为列表中的项目赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆