序言中的随机播放

编程入门 行业动态 更新时间:2024-10-27 14:25:45
本文介绍了序言中的随机播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 prolog 中编写一个过程,如果 L1 = [1,2,3] 和 L2 = [4,5,6] 然后 L3 = [1,4,2,5,3,6]

I'm trying to write a procedure in prolog where if L1 = [1,2,3] and L2 = [4,5,6] then L3 = [1,4,2,5,3,6]

所以 shuffle([1,2,3],[4,5,6],[1,4,2,5,3,6])

到目前为止我有这个:

shuffle([X],[Y],[X,Y]). shuffle([X|Xs],[Y|Ys],_) :- shuffle(Xs,Ys,Z), shuffle(X,Y,Z).

这是我第一次尝试编写 prolog 代码,所以我仍在努力思考语法、规则和所有内容.

This is my first attempt at writing prolog code so I'm still trying to wrap my head around the syntax, rules and everything.

我理解逻辑,我只是​​不确定如何实现它,所以任何帮助将不胜感激!

I understand the logic, I'm just not sure how to implement it so any help would be greatly appreciated!

谢谢!

我已经弄明白了.如果有人感兴趣,这是解决方案:

I've figured it out. Here's the solution if anyone's interested:

shuffle([X],[Y],[X,Y]). shuffle([X|Xs],[Y|Ys],[Z1,Z2|Zs]) :- shuffle([X],[Y],[Z1,Z2]),shuffle(Xs,Ys,Zs).

推荐答案

shuffle([], B, B). shuffle([H|A], B, [H|S]) :- shuffle(B, A, S).

在这类问题中,困难的部分通常不是 Prolog,而是找出解决它的最简单的递归关系.

In this kind of problems, usually the difficult part is not Prolog but identifying the simplest recursive relation that solves it.

更多推荐

序言中的随机播放

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

发布评论

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

>www.elefans.com

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