如何从术语中获取简单元素?

时间:2018-12-22 14:28:57

标签: prolog

我想从term中获取简单元素,并将其返回到列表中。

我的代码:

    ID  Element Data_Value
Date            
2014-11-12  USW00094889 TMAX    22
2009-04-29  USC00208972 TMIN    56
2008-05-26  USC00200032 TMAX    278
2005-11-11  USC00205563 TMAX    139
2014-02-27  USC00200230 TMAX    -106
2010-10-01  USW00014833 TMAX    194
2010-06-29  USC00207308 TMIN    144
2005-10-04  USC00203712 TMAX    289
2007-12-14  USW00004848 TMIN    -16
2011-04-21  USC00200220 TMAX    72
2013-01-16  USC00205822 TMAX    11
2008-05-29  USC00205822 TMIN    28

此查询有效

getSimple(T, R) :- 
  compound(T), 
  T =.. [_|R]

示例:

getSimple(f(1, 2, a), X)

此查询返回错误结果:

X = [1, 2, a]

示例:

getSimple(f(2, 3, g(a)), X)

预期:

X = [2, 3, g(a)]

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的那样,该解决方案需要递归并且需要更多的参与。

这是一种可能的解决方案,其中我为您留出了空白点以供梳理:

gem5 Simulator System.  http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 compiled Oct 27 2018 00:36:02
gem5 started Dec 22 2018 18:16:40
gem5 executing on Dan
command line: ./build/X86/gem5.opt configs/example/se.py -c /home/dan/SPEC2006/benchspec/CPU2006/401.bzip2/exe/bzip2_base.ia64-gcc42 -i /home/dan/SPEC2006/benchspec/CPU2006/401.bzip2/data/test/input/dryer.jpg

Could not import 03_BASE_FLAT
Could not import 03_BASE_NARROW
Global frequency set at 1000000000000 ticks per second
warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (4096 Mbytes)
0: system.remote_gdb.listener: listening for remote gdb #0 on port 7000
**** REAL SIMULATION ****
info: Entering event queue @ 0.  Starting simulation...
panic: Tried to write unmapped address 0xffffedd8. Inst is at 0x400da4
 @ tick 5500
[invoke:build/X86/arch/x86/faults.cc, line 160]
Memory Usage: 4316736 KBytes
Program aborted at tick 5500
Aborted (core dumped)

尝试填写上面的空白。请注意,“递归”表示您将要再次致电% We'll use an auxiliary predicate that handles a list of terms term_args(Term, Args) :- term_list_args([Term], Args). % This predicate recursively handles a list of terms, each of which is or is not be compound % ...so you have to check! term_list_args([Term|Terms], Args) :- ( compound(Term) -> Term =.. [_|TermArgs], % If this term is a compound term ___, % Recursively process the term's arguments (TermArgs) ___, % Recursively process the remaining terms (Terms) ___ % append the results of the two queries above to obtain Args ; % Otherwise, if Term is not compound ___, % Recursively process the remaining terms (Terms) - obtaining TermArgs Args = [Term|TermArgs] ). term_list_args([], []). % base case: An empty list of terms corresponds to an empty arg list

相关问题