Prolog 使用列表和递归 [英] Prolog working with list and recursion

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

问题描述

我是 Prolog 的新手,正在努力学习.我遇到了一个问题,我正在尝试运行它,但我认为我犯了一个根本性的错误,我的代码无法正常工作.我正在尝试编写一个带有两个参数的谓词.第一个参数是一个列表,第二个参数是一个列表,其中的成员是第一个列表的成员,重复两次.

I am new in Prolog and trying to learn it. I came across a question and I am trying to run it but I think I am making a fundamental mistake and my code does not work properly. I am trying to write a predicate that takes two arguments. The first argument is a list and the second argument is a list which members are the members of the first list, repeated twice.

这是我的代码:

twice([],[]).
twice([X|Taila],[X,X|Tailb]) : twice(Taila,Tailb).

例如

twice([z,4,hello],X).

应该返回

X = [z,z,4,4,hello,hello]).

推荐答案

我得到了答案

twice([], []).
twice([X|Taila], [X, X|Tailb]) :- twice(Taila, Tailb).

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

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