Prolog列表具有未实例化的尾巴,需要摆脱它 [英] Prolog list has uninstantiated tail, need to get rid of it

查看:30
本文介绍了Prolog列表具有未实例化的尾巴,需要摆脱它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Prolog程序.它产生正确的输出(列表),但列表末尾有未实例化的变量.我做错了事,不确定如何摆脱它.

I'm working on a Prolog program. It produces the correct output (a list) but the list has an uninstantiated variable at the end. I am doing something wrong, and am unsure how to get rid of it.

这是代码:

plaatsingen([],_).
plaatsingen([Prod/Hoev|Rest],[Order|Plaatsing]) :-
    prijs(Lev,Prod,Prijs),
    Kost is Prijs*Hoev,
    Order = Lev/Prod/Kost,
    plaatsingen(Rest,Plaatsing).

prijs(delhaize, zeep, 8).
prijs(delhaize, prei, 10).
prijs(delhaize, zout, 6).
prijs(carrefour, prei, 9).
prijs(carrefour, soep, 19).
prijs(champion, zeep, 7).
prijs(champion, prei, 11).
prijs(champion, pinda, 6).

这是输入和输出:

    41 ?- plaatsingen([zeep/10, prei/14],P).
P = [delhaize/zeep/80, delhaize/prei/140|_G4160] .

推荐答案

如果要摆脱它,则需要对其进行处理:

If you want to get rid of it, you need to get a handle on it:

plaatsingen([],G,G).
plaatsingen([Prod/Hoev|Rest],[Order|Plaatsing],G) :-
    prijs(Lev,Prod,Prijs),
    Kost is Prijs*Hoev,
    Order = Lev/Prod/Kost,
    plaatsingen(Rest,Plaatsing,G).

G 就是这样的句柄.现在,您可以将未实例化的尾部明确地放回原处,并将其设置为任何值:

G is such a handle. Now you get back in it the uninstantiated tail, explicitly, and can set it to anything:

plaatsingen(In, Out, Z), Z=[].

将列表的末尾设置为空列表.或者,您可以使用

sets the end of list to the empty list. Or you can pre-set it, with

plaatsingen(In, Out, [end, of, list]).

或将其保留为免费的logvar,这是差异列表的机制.

or leave it as a free logvar, which is the mechanism of difference lists.

这篇关于Prolog列表具有未实例化的尾巴,需要摆脱它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆