包装列表中的每个元素

时间:2019-09-23 02:22:34

标签: prolog swi-prolog maplist

如何包装列表中的每个元素?

我有这样的(2*3*4*...)^6

如何使用以下输出列出列表:2^6 * 3^6 * 4^6 * ...

我在考虑使用maplist做一些简单的事情,但是我不确定如何在第一个参数中将参数发送给函数。

simplify(X^Y,R):- X=..[*|Args], maplist(?^Y, Args, Args2), R=..[*|Args2], !.

:- simplify((2*x)^6, (2^6) * (x^6)). %should be true

顺便使用Swi-prolog

1 个答案:

答案 0 :(得分:1)

您的输入数据结构不包含任何列表,它是一个嵌套的结构,在叶子上有数字:

private void Timer1_Tick(object sender, EventArgs e)
        {
            Hide();
            Main_Form MainForm = new Main_Form();
            MainForm.Show();
            timer1.Stop();
        }

因此,maplist在这里用处不大。相反,编写一个简单的递归谓词遍历(并重建)递归数据结构:

?- write_canonical(2*3*4*5).
*(*(*(2, 3), 4), 5)

代码结构遵循数据结构。示例运行

simplify(N, Exp, N^Exp) :-
    number(N).
simplify(L*R, Exp, LExp*RExp) :-
    simplify(L, Exp, LExp),
    simplify(R, Exp, RExp).
相关问题