Prolog-列表成字符串 [英] Prolog - List into String
问题描述
我已经在这里寻找解决方案了,但是什么也没做,所以我决定问一下.
im done of searching here for a solution but nothing so i decided to ask.
因此,我有一个查询,该查询在列表中返回如下内容:
So, i have a query that returns in a list something like this:
List = [6-"read",3-"magazines",3-"music",1-"sport"].
我需要执行转换,这样我才能得到这样的列表:
And i need to perform a transformation so do i can get the list like this:
List = [read,magazines,music,sport].
or
List = ["read","magazines","music","sport"].
为此,我认为我应该先将其传递给字符串以取出数字和-".但是我为此苦苦挣扎.
For that i think i should pass first the to a string to take out the numbers and the '-'. But im struggling with that.
希望有人可以帮助我!谢谢
Hope someone can help me! Thanks
推荐答案
这看起来像是作业,所以我不会为您提供完整的实现.您正在寻找的是规则头中的模式匹配:
This looks like homework, so I will not give you the full implementation. What you are looking for is pattern matching in a rule head:
fst_pair(X, pair(X,Y)).
-
运算符只是一个中缀函数符号,因此您可以编写
The -
operator is just an infix function symbol such that you could write
fst(X, X-Y).
使用这种模式匹配,可以很容易地在列表上编写一个递归谓词.对于空列表,它必须具有基本大小写,对于列表的首尾必须具有步进大小写:
Using this kind of pattern matching it should be easy to write a recursive predicate over the list. It must have a base case for the empty list and a step case for a head followed by the tail of the list:
fsts_list([], []).
fsts_list([ ... | TailFirsts ], [... | Pairs] ) :- % replace ... by some terms
% possibly insert some predicates here, depending on what you do above
fst_lists(TailFirsts, Pairs).
解决愉快!
这篇关于Prolog-列表成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!