- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个 playground 示例,灵感来 self 曾经做过的一项真实任务(复杂得多)。基本流程是从顺序文件中读取记录。一些记录包含需要检查以前的记录以计算值的命令。
这个解决方案的缺点是它需要一个额外的列表,因此需要额外的重复存储。该额外列表在以下代码中称为 REMEMBER。这个例子有一个简单的记录结构,只包含一个数据值,所以复制 REMEMBER 列表中的所有内容并不是真正的问题。但是请假设实际任务涉及更复杂的记录结构,以至于复制 REMEMBER 列表中的所有内容是非常不可取的。
我倾向于使用双向链表,但是根据此链接上的讨论 Doubly Linked List in Prolog似乎这不是 Prolog 做事的方式。因此,我很想知道 Prolog 的处理方式应该是什么。
/*
A file contains sequential records.
There are two types of record.
A data record provides a data value.
An average record provides a count and is a request for an average of the last count data values.
The parse dcg below parses a list from the data file.
The report dcg uses that list to generate a report.
After parse the list looks like this:
[value=5.9,value=4.7,value=7.5,average=3,value=9.0,value=1.1,value=8.3,average=5,value=7.1,value=1.3,value=6.7,value=9.9,value=0.5,value=0.3,value=1.5,value=0.2,average=7,value=2.2,value=7.8,value=2.5,value=4.5,value=2.4,value=9.7,average=4,value=5.2,value=8.5,value=2.2,value=8.0,value=0.7].
An example report looks like this:
[[count=3,total=18.1,average=6.033333333333333],[count=5,total=30.599999999999998,average=6.12],[count=7,total=20.400000000000002,average=2.9142857142857146],[count=4,total=19.1,average=4.775]].
*/
:- use_module(library(dcg/basics)).
:- use_module(library(readutil)).
:- use_module(library(clpfd)).
:- use_module(library(clpr)).
dospy
:-
spy(report),
spy(average),
leash(-all).
:- initialization main.
report(LIST)
-->
% the report starts with nothing to REMEMBER.
report(LIST,[]).
report([value=VALUE|LIST],REMEMBER)
-->
% value in the LIST goes into REMEMBER.
report(LIST,[value=VALUE|REMEMBER]).
report([average=COUNT|LIST],REMEMBER)
-->
% request for average in the LIST.
average(REMEMBER,COUNT),
report(LIST,REMEMBER).
report([],_REMEMBER)
% the LIST is empty so the report is done.
--> [].
average(REMEMBER,COUNT)
-->
% the average starts at 0 then accumulates for COUNT values from REMEMBER.
average(REMEMBER,COUNT,0,0.0).
average([value=VALUE|REMEMBER],COUNT,AT,TOTAL)
-->
% found needed value in the REMEMBER.
clpfd( AT #\= COUNT ),
clpfd( AT_NEXT #= AT + 1 ),
clpr( TOTAL_NEXT = TOTAL + VALUE ),
average(REMEMBER,COUNT,AT_NEXT,TOTAL_NEXT).
average(_REMEMBER,COUNT,COUNT,TOTAL)
-->
% all of the needed value have been seen so calculate and add to report.
clpr( AVERAGE = TOTAL / COUNT ),
[[count=COUNT,total=TOTAL,average=AVERAGE]].
% now the part that does the parse of the data file.
parse(LIST) --> parse(data,LIST).
parse(LIST) --> parse(average,LIST).
parse(LIST) --> parse(end,LIST).
parse(data,[value=FLOAT|LIST])
-->
"data", whites, float(FLOAT), blanks, !,
parse(LIST).
parse(average,[average=COUNT|LIST])
-->
"average", whites, integer(COUNT), blanks, !,
parse(LIST).
parse(end,[])
-->
[].
clpr( CLPR )
-->
{ clpr:{ CLPR } }.
clpfd( CLPFD )
-->
{ CLPFD }.
main :-
system:( read_file_to_codes("doubly_motivation_data.txt",CODES,[]) ),
prolog:( phrase(parse(LIST),CODES) ),
system:( writeq(LIST),writeln(.) ),
prolog:( phrase(report(LIST),REPORT) ),
system:( writeq(REPORT),writeln(.) ).
/* doubly_motivation_data.txt
data 5.9
data 4.7
data 7.5
average 3
data 9.0
data 1.1
data 8.3
average 5
data 7.1
data 1.3
data 6.7
data 9.9
data 0.5
data 0.3
data 1.5
data 0.2
average 7
data 2.2
data 7.8
data 2.5
data 4.5
data 2.4
data 9.7
average 4
data 5.2
data 8.5
data 2.2
data 8.0
data 0.7
*/
最佳答案
遵循 Logtalk + SWI-Prolog 解决方案,不需要双链表的任何具体化。只需要一个堆栈,使用列表简单地实现:
------------ reports.lgt ------------
% load the required modules
:- use_module(library(dcg/basics), []).
% ensure desired interpretation of double-quoted text
:- set_prolog_flag(double_quotes, codes).
% optimize the generated code
:- set_logtalk_flag(optimize, on).
:- object(reports).
:- public(process/2).
:- uses(list, [take/3]).
:- uses(numberlist, [sum/2]).
:- uses(reader, [file_to_codes/2]).
:- use_module(dcg_basics, [blanks//0, whites//0, integer//1, float//1]).
process(File, Report) :-
file_to_codes(File, Codes),
phrase(report(Report), Codes).
report(Report) -->
data(Value),
report(Report, [Value]).
report([], _) -->
eos.
report([Record| Records], Stack) -->
average(Count),
{compute_record(Count, Stack, Record)},
report(Records, Stack).
report(Records, Stack) -->
data(Value),
report(Records, [Value| Stack]).
average(Count) -->
"average", whites, integer(Count), blanks.
data(Value) -->
"data", whites, float(Value), blanks.
compute_record(Count, Stack, r(Count,Total,Average)) :-
take(Count, Stack, Values),
sum(Values, Total),
Average is Total / Count.
:- end_object.
-------------------------------------
使用问题中的数据文件调用示例:
?- {library(types_loader), library(reader_loader), reports}.
...
?- reports::process('doubly_motivation_data.txt', Report).
Report = [r(3, 18.1, 6.033333333333334), r(5, 30.599999999999998, 6.119999999999999), r(7, 20.400000000000002, 2.9142857142857146), r(4, 19.1, 4.775)] .
正如您所注意到的,我对报告使用了比列表列表更合理的表示形式。可以通过组合 take/3
来编写更有效的解决方案。和 sum/2
通过避免使用长度为 Count
的堆栈前缀来调用自定义谓词被遍历两次并创建一个临时值列表。例如:
compute_record(Count, Stack, r(Count,Total,Average)) :-
compute_record(0, Count, Stack, 0, Total),
Average is Total / Count.
compute_record(Count, Count, _, Total, Total) :-
!.
compute_record(Count0, Count, [Value| Stack], Total0, Total) :-
Count1 is Count0 + 1,
Total1 is Total0 + Value,
compute_record(Count1, Count, Stack, Total1, Total).
从示例数据文件来看,文件似乎可以以计算文件中所有 值的平均值的请求结束。因此,report//2
非终端必须保留整个堆栈,直到处理完所有数据文件。
关于list - 使用 Prolog 读取记录列表并执行有关先前记录的持续计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54407104/
我想使用 R 预定义这样的列表 DATA<-list( list(list(),list(),list()), list(list(),list(),list()), list(list(),l
如何将一个列表添加到另一个列表,返回一个列表的列表? foo :: [a] -> [a] -> [[a]] 例如,我想要的结果是: foo [1,2] [3,4] 将是 [[1,2], [3,4]]。
我还没有在这里找到类似问题的解决方案,所以我会寻求你的帮助。 有 2 个列表,其中之一是列表列表: categories = ['APPLE', 'ORANGE', 'BANANA'] test_re
这个问题不同于Converting list of lists / nested lists to list of lists without nesting (这会产生一组非常具体的响应,但无法解决
原始列表转换为 List正好。为什么原始列表的列表不能转换为 List 的列表? { // works List raw = null; List wild = raw; } {
在下面的代码中,get()被调用并将其结果分配给类型为 List> 的变量. get()返回 List>并在类型参数为 T 的实例上调用设置为 ? ,所以它应该适合。 import java.util
原始列表转换为 List正好。为什么原始列表的列表不能转换为 List 的列表? { // works List raw = null; List wild = raw; } {
在insufficiently-polymorphic 作者说: def foo[A](fst: List[A], snd: List[A]): List[A] There are fewer way
我有下面的代码有效。 class ListManipulate(val list: List, val blockCount: Int) { val result: MutableList>
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
在 scala (2.9) 中转换列表列表的最佳方法是什么? 我有一个 list : List[List[A]] 我想转换成 List[A] 如何递归地实现这一点?或者还有其他更好的办法吗? 最佳答案
我编写了这个函数来确定给定元素是否存储在元组列表的列表中,但目前它只搜索第一个列表。我将如何搜索其余列表? fun findItem (name : command, ((x,y)::firstlis
我创建了一个类名 objectA,它有 4 个变量:约会时间;字符串文本;变量 1,变量 2 我需要创建一个 ObjectA() 列表。然后首先按时间对它们进行分组,其次按 var1,然后按 var2
我有一套说法 char={'J','A'} 和列表的列表 content = [[1,'J', 2], [2, 'K', 3], [2, 'A', 3], [3,'A', 9], [5, 'J', 9
我有以下列表 List >>> titles = new ArrayList >>> ();我想访问它的元素,但我不知道该怎么做.. 该列表有 1 个元素,它又包含 3 个元素,这 3 个元素中的
转换 List[List[Long]] 的最佳方法是什么?到 List[List[Int]]在斯卡拉? 例如,给定以下类型列表 List[List[Long]] val l: List[List[Lo
我有一个来自 Filereader (String) 的 List-List,如何将其转换为 List-List (Double):我必须返回一个包含 line-Array 的第一个 Values 的
我收集了List> 。我需要将其转换为List> 。这是我尝试过的, List> dataOne = GetDataOne(); var dataTwo = dataOne.Select(x => x
这个问题在这里已经有了答案: Cannot convert from List to List> (3 个答案) 关闭 7 年前。 我没有得到这段代码以任何方式编译: List a = new Ar
这个问题在这里已经有了答案: Cannot convert from List to List> (3 个答案) 关闭 7 年前。 我没有得到这段代码以任何方式编译: List a = new Ar
我是一名优秀的程序员,十分优秀!