简体   繁体   English

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). 

Happy solving! 解决愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM