结合两个列表的序言 [英] Prolog Combining Two Lists

查看:94
本文介绍了结合两个列表的序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是prolog的新手,并希望在以下问题上获得帮助:

I am new to prolog and would appreciate any help on the following question:

我需要编写一个程序,该程序接受两个列表并将第二个列表附加到第一个列表和显示此新列表及其长度。我知道prolog可能具有一些内置函数来简化这些操作……但是我不想使用它们。

I need to write a program that accepts two lists and appends the second to first and displays this new list and its length. I know that prolog might have some built in functions to make this all easier...but I do not want to use those.

例如: newlist([a,b,c],[d,e,f],L3,Le)。将返回 L3 = [a,b,c,d,e ,f] Le = 6

这是我到目前为止的内容:

Here is what I have so far:

newlist([],List,List,0) 

newlist([Element|List1],List2,[Element|List3],L) :- newlist(List1,List2,List3, LT), L is LT + 1.

这可以正确地进行附加,但是我只能得到第一个列表的长度,而不是合并列表的长度。我有办法将第二个列表的长度添加到第一个列表中以获得合并列表的长度吗?

This does the appending correctly but I can only get the length of the first list instead of the combined list. Is there a way for me to add the second list's length to the first to get the combined list length?

谢谢,如果这个问题很简单,请谅解...我是新来的。

Thanks, and sorry if this question is rather easy...I am new.

推荐答案


有没有办法将第二个列表的长度添加到首先要获得合并列表的长度?

Is there a way for me to add the second list's length to the first to get the combined list length?

您应该替换:

newlist([],List,List,0).

具有:

newlist([],List,List,X):-length(List,X).

这篇关于结合两个列表的序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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