Prolog 使用列表和递归

编程入门 行业动态 更新时间:2024-10-22 13:40:55
本文介绍了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 使用列表和递归

本文发布于:2023-11-30 03:15:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   列表   Prolog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!