序言中的递归(在列表上) [英] recursion in prolog (on lists)

查看:48
本文介绍了序言中的递归(在列表上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我了解执行递归序言函数的基础知识吗..

can someone please help me just w/ the basics on performing recursive prolog functions..

append([],X,X). % base
append([X|Y],Z,[X|W]) :- append(Y,Z,W). %recursive

% base case
addup([], 0). % sum of the empty list of numbers is zero

% recursive case: if the base-case rule does not match, this one must:
addup([FirstNumber | RestOfList], Total) :-
    addup(RestOfList, TotalOfRest),   % add up the numbers in RestOfList
    Total is FirstNumber + TotalOfRest.

有人可以用英语或 C/C++/Java 解释一下……步骤如何.我实际上更喜欢看到类似 append 或 reverse 之类的东西..我主要是在操作变量列表而不是整数..(我已经尝试了 10 次这样的 append .. 呃).

Can someone explain either in English or in C/C++/Java whatever.. how the steps. I actually would prefer to see something like append or reverse.. I'm mostly just manipulating lists of variables instead of integers.. (I've tried to work through append like 10 times.. ugh).

推荐答案

免费的在线书籍Learn Prolog Now"有一节专门解释 append 执行的步骤:

The free online book "Learn Prolog Now" has a section dedicated to explaining the steps that append performs:

http:///cs.union.edu/~stregnk/learn-prolog-now/html/node47.html#subsec.l6.defining.append

这篇关于序言中的递归(在列表上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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