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

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

问题描述

我是 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

eg: newlist([a,b,c],[d,e,f],L3,Le). would return L3=[a,b,c,d,e,f] and Le=6

这是我目前所拥有的:

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).

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

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