# 知识库递归编程java和prolog代码；逻辑语言Prolog简介(附24555字PDF发“递归prolog简介”下载)

## 知识库递归编程java和prolog代码；逻辑语言Prolog简介(附24555字PDF发“递归prolog简介”下载)

A  逻辑学形式化及递归的prolog编程(4860字)

1. 逻辑学形式化方法简介

2. SWI-Prolog的递归

B  逻辑编程语言Prolog简介(18718字)

1. SWI-Prolog

2. Prolog

Appx(1236字).数据简化DataSimp社区简介

A  逻辑学形式化及递归的prolog编程(4860)

1. 逻辑学形式化方法简介

1.1 形式化方法(formal methods)

Description logic is a formal language for representingknowledge and it is a decidable subset of first-order logic. There is a heateddispute between the logic of language in broad sense and the one in narrowsense on the application of the method of formalization. Using temporal logicand algebra, a formal requirement language was presented and used to describethe formal hierarchy requirements for cryptographic protocols.

1.2 递归和递推概念

1.3 递归的java算法设计

1.3.1算法举例1



publicclass Fibonacci {



   staticint fun(int n){



        if(n == 1 || n == 2){



            return1 ;



        }else{



            return fun(n-1) + fun(n-2) ;



        }



    }



    publicstaticvoidmain(String[] args) {



        for(int i = 1 ; i <= 15 ; ++i)



        System.out.println(fun(i));



    }



}



static int fun2(int n){



int a[] = new int[20] ;



a[1] = 1 ;



a[2] = 1 ;



for(int i=3 ; i<=n ;i++){



a[i] = a[i-1] + a[i-2] ;



}



return a[n] ;



}



1



1



2



3



5



8



13



21



34



55



89



144



233



377



610

1.3.2 算法举例2



public class Sum {



static int fun(int n){



if( n == 1){



return 1 ;



}else{



return fun(n-1) + n ;



}



}



public static void main(String[] args) {



// TODO Auto-generated method stub



System.out.println(fun(100));



}



}



static int fun2(int n){



int a[] = new int [200] ;



a[1] = 1 ;



for(int i=2 ; i<=n ; i++){



a[i] = a[i-1] + i ;



}



return a[n] ;



}



5050

1.3.3 算法举例3





static int fun(int n){



if(n == 1){



return 1 ;



}else if(n == 2){



return 2 ;



}else{



return fun(n-1) + fun(n-2) ;



}



}



public static void main(String[] args) {



// TODO Auto-generated method stub



System.out.println(fun(5));



}



}



static int fun2(int n){



int a[] = new int [200] ;



a[1] = 1 ;



a[2] = 2 ;



for(int i=3 ; i<=n ;i++){



a[i] = a[i-1] + a[i-2] ;



}



return a[n] ;



}



8

2. SWI-Prolog的递归

Prolog是一门声明式编程语言，当你处理事物集合时，如列表或树，你会经常使用递归而不是迭代。递归是Prolog中最主要而又最难于掌握的概念之一，若要控制回溯度还与截断有关。

2.1 牛刀小试——寻找祖先和后代的程序

2.2 渐入佳境——利用递归计算阶乘

n! = n ×(n - 1) ×(n - 2) ×……× 3 × 2 × 1

n! = n ×(n-1)!

0! = 1(停止条件)

n! = n x(n - 1)!(递归定义)

2.3 趁热打铁——斐波那契数列

1，1，2，3，5，8，13，21，34，55，……

f(1) = 1(两个停止条件)

f(2) = 1

f(n) = f(n-1) + f(n-2)(递归规则)

2.4 总结

某些初始计算,

递归谓词,

某些最终运算.

(注1：资料来自百度、CSDN等[4-6]。)

## B逻辑编程语言Prolog简介(18718字)

1.SWI-Prolog

News: SWI-Prolog on Android TermuxSearch Documentation:

Robust, mature, free. Prolog for the realworld.



SWI-Prolog提供全面的免费Prolog环境。自1987年成立以来，SWI-Prolog开发一直受到现实世界应用需求的驱动。SWI-Prolog广泛用于研究和教育以及商业应用。加入已下载SWI-Prolog的超过一百万用户。更多 ...

SWI-Prolog offers acomprehensive free Prolog environment. Since its start in 1987, SWI-Prologdevelopment has been driven by the needs of real world applications. SWI-Prologis widely used in research and education as well as commercial applications.Join over a million users who have downloaded SWI-Prolog. more ...

http://www.swi-prolog.org/features.html

News: SWI-Prolog Class!Search Documentation:

1.1 概述Overview

SWI-Prolog是Prolog语言的多功能实现。尽管SWI-Prolog主要在教育领域受到欢迎，但其发展主要受应用程序开发需求的驱动，通过支持许多文档类型和(网络)协议以及与C的全面低级接口(其为C++，Java(捆绑)，C＃，Python等(外部可用)的高级接口的基础)等与其他IT组件的丰富接口促进。数据类型扩展(如dicts和strings)以及对Unicode和无界整数的完全支持简化了与其他组件数据的平滑交换。

SWI-Prolog旨在实现可扩展性。它对多线程的强大支持有效地利用了多核硬件，并简化了并发应用程序中的嵌入。它的即时索引(JITI)为具有数百万个子句的谓词提供透明和有效的支持。

SWI-Prolog统一了Prolog社区中开发的核心语言的许多扩展，例如表格、约束、全局变量、破坏性赋值、分隔连续和交互者。

SWI-Prolog提供各种开发工具，其中大部分可以随意组合。本机系统提供了一个用Prolog编写的编辑器，它是Emacs的相似克隆。它基于Prolog系统本身对代码的实时分析提供语义突出显示。补充工具包括图形调试器、分析器和交叉引用器。或者，有一种GNU-Emacs模式，Eclipse插件称为PDT和VSC插件，每种插件都可以与本机图形工具结合使用。最后，SWISH提供了一个计算笔记本和基于Web的IDE。SWISH是一种多功能工具，可以配置和扩展以适应许多不同的场景。

SWI-Prolog提供了一个名为的附加分发和安装机制。包是具有最小组织约定的目录，和描述源、版本、依赖性和自动升级支持的控制文件。可以使用pack_install/1包从存档，GIT仓库或URL安装包。包用于在社区中共享代码。包装系统已经发展了几个生态系统，用于处理类型，协同处理等。

SWI-Prologis a versatile implementation of the Prolog language. Although SWI-Prologgained its popularity primarily in education, its development is mostly drivenby the needs for application development. This is facilitatedby a rich interface to other IT components by supporting many document typesand(network) protocols as well as a comprehensive low-level interface to C thatis the basis for high-level interfaces to C++, Java(bundled), C#, Python, etc(externallyavailable). Data type extensions such as dictsand stringsas well as full support for Unicode and unbounded integers simplify smoothexchange of data with other components.

SWI-Prologaims at scalability. Its robust support for multi-threadingexploits multi-core hardware efficiently and simplifies embedding in concurrentapplications. Its Just In Time Indexing(JITI) provides transparent andefficient support for predicates with millions of clauses.

SWI-Prologunifies many extensions of the core language that have beendeveloped in the Prolog community such as tabling, constraints, globalvariables, destructive assignment, delimited continations andinteractors.

SWI-Prologoffers a variety of development tools, most of which may becombined at will. The native system provides an editor written in Prolog thatis a close clone of Emacs. It provides semantic highlighting based onreal time analysis of the code by the Prolog system itself. Complementary toolsinclude a graphical debugger, profiler and cross-referencer. Alternatively,there is a mode for GNU-Emacs and, Eclipse plugin called PDTand a VSC plugin,each of which may be combined with the native graphical tools. Finally, a computationalnotebook and web based IDE is provided by SWISH. SWISH is a versatile tool thatcan be configured and extended to suit many different scenarios.

SWI-Prologprovides an add-on distribution and installation mechanism called packs.A pack is a directory with minimal organizational conventions and a controlfile that describes the origin, version, dependencies and automatic upgradesupport. Packs can be installed from an archive, GIT repository or URL using pack_install/1.Packs are used to share code in the community. The pack system has grown acouple of eco systems for dealing with types, coroutining, etc.

1.2 服务器应用Server applications

SWI-Prolog配备了广泛的Web服务器(HTTP)框架，可用于提供(REST)服务和基于HTML5+CSS+JavaScript的最终用户应用程序。Pengines(Prolog引擎)允许客户端使用通用API对远程服务器上的客户端提供的程序运行查询。这些程序可以在沙箱中执行。

SWI-Prolog具有几个独特的功能，可以减少重启服务器以修复错误或注入诊断代码的需要。它的增量编译与通常的本地和可回溯数据结构(撤消)相结合，允许在不重新启动的情况下修补程序。SWI-Prolog允许安全地从另一个线程重新加载运行代码，前提是运行谓词的签名不会更改。这意味着允许添加，删除和修改子句，但不允许添加，删除或重新排序参数。但是，此功能对于热修复服务器或将诊断代码注入服务器而不重新启动非常有用。

SWI-Prologis equipped with an extensive web server(HTTP) framework thatcan be used both for providing(REST) services and end-user applications basedon HTML5+CSS+JavaScript. Pengines(Prolog engines) allow clients to run queriesagainst a client-provided program on a remote server using a generic API. Suchprograms can be executed in a sandbox.

For(web)server support SWI-Prolog provides scalable multi threading.We measured an 80 times speedup running on a 128 core power pc system. Thisfeature makes SWI-Prolog attractive for CPU intensive server tasks wheremultiple clients require access to a large shared and possibly dynamic dataset.Note that many other high-level languages such as Python and Node.js only use asingle core and switch between tasks based on blocking I/O. This approachgenerally provides good resource usage for I/O intensive services with a highnumber of connections but a task blocking on a computation delays all otherclients. As SWI-Prolog's global garbage collectors_ for atoms andremoved dynamic clauses run fully asynchronous in a dedicatedthread, its(soft) real time behaviour is excellent.

SWI-Prologhas several unique features that reduce the need to restart servers for fixingbugs or injecting diagnostic code. Its incremental compilationcombined with generally local and backtrackable data structures(undo) allowsfor patching the program without restarting. SWI-Prolog allowsfor reloading running code from another thread safely, provided the signatureof the running predicates is not changed. This implies it is allowed to add,remove and modify clauses but it is not allowed to add, remove orreorder arguments. Still, this feature is valuable for hot-fixingservers or inject diagnostic code to a server withoutrestarting.

1.3 Prolog作为统一语言Prolog as unifying language

Prolog是域特定语言(DSL)的合适语言。SWI-Prolog通过Prolog语言的语法扩展改进了这种支持。可以明确处理的有效语法的示例是[1][2]，point{x:1, y:1}和function()。此外，准引用允许与任意语言的语法进行安全和清晰的集成。例如，以下语句指定了Prolog值X的安全插值的JavaScript片段。



{|javascript(X)||var x = X;|}

AlthoughProlog is widely recognised as a special purpose language for tasks suchas rule evaluation we consider it primarily a platform that is suitable to beused as glue between various components. The main reason for this isthat data is at the core of many modern applications whilethere is a large variety in which data is structured and stored. Classicalquery languages such as SQL, SPARQL, XPATH, etc. can each deal with one suchformat only, while Prolog can provide a concise and natural query language foreach of these formats that can either be executed directly or be compiled intodedicated query language expressions. Prolog's relational paradigmfits well with tabular data(RDBMS), while optimized support forrecursive code fits well with tree and graph shaped data(RDF).

Prologis a suitable language for Domain Specific Languages(DSL). SWI-Prologhas improved this support by syntactical extensions to the Prolog language.Examples of valid syntax that can be processed unambiguously are a[1][2],point{x:1,y:1} and function(). In addition, quasiquotations allow for safe and clean integration with the syntax ofarbitrary languages. For example, the following statement specifies a fragmentof JavaScript with a safe interpolation of the Prolog valueX.



{|javascript(X)||var x = X;|}

1.4 功能列表Feature list

Belowis a list of what we consider key features of SWI-Prolog with links to therelevant documentation.

1.4.1 引擎Engine

•快速编译。例如，在14秒内从Prolog源加载WordNet 3.0或从快速加载文件格式加载0.4秒(参见qcompile/1)。WordNet源计数为821,515行。系统：Intel i7-3770上的Ubuntu 16.04,32Gb内存。

•强大且无内存泄漏。用于多个24x7运行的服务器(包括此Web服务)。

•小。完整的开发环境，包括图形，库和许多接口包，需要大约100MB的硬盘。内核大约是1.4MB(Ubuntu 16.04 .so文件)

•适用于大型应用程序。程序大小，原子长度，术语arity或整数值没有限制。具有许多(索引)事实的谓词没有性能下降。

•对任何参数的静态和动态代码进行即时索引，大大简化了处理多模式关系的条款。从7.5版开始，支持多参数JITI。如果没有选择性单参数索引，这将为两个参数的组合值创建索引。从版本7.7开始，我们还支持对复合术语的参数进行索引。此功能显着提高了语法规则(DCG)中处理终端的性能。

•基于GMP库的无界整数和有理数运算。

•好东西：模块(向上兼容Quintus和SICStus)，垃圾收集(对C/C ++透明 - 代码，包括原子和子句垃圾收集)，最后调用优化，运行时堆栈的动态扩展，异常处理(包括用于捕获和抛出异常的C/C ++接口)。

•归因变量，coroutining(冻结/2，当/2，dif/2)，全局变量，循环项。

•标志控制的发生检查处理(假/真/错误)请参见current_prolog_flag/2。发生检查的有效实现仅检验新绑定不引入循环。

•内部UNICODE字符集处理。适用于网络和国际应用。

•多线程支持：在同一数据库上运行多个预先安排好的prolog引擎。

•引擎，也称为交互器，提供协同程序，可用于状态累积和大规模并发，例如群体智能和模拟。

•定界延续是新控制结构和实现方面编程的强大构建块。

•Tabling(SLG分辨率)提供了更强大的解析技术，用于解决复杂相关规则上的查询并保证终止。

·      fast compilation. E.g., loads WordNet 3.0in 14 seconds from the Prolog source or 0.4 seconds from quick load fileformat(see qcompile/1).The WordNet source counts 821,515 lines. System: Ubuntu 16.04 on Intel i7-3770,32Gb memory.

·      Robust and free of memory leaks. In use for severalservers that run 24x7(including this web service).

·      Small. The full development environment, includinggraphics, libraries and many interface packages, requires approximately 100MBhard disk. The kernel is about 1.4MB(Ubuntu 16.04 .so file)

·      Scales well for large applications. No limits onprogram size, atom length, term arity or integer values. No performancedegradation on predicates with many(indexed) facts.

·      Just-In-Time indexing of both static and dynamic code on anyargument greatly simplifies handling multi-moded relations with many clauses.As of version 7.5, multi-argument JITI is supported. Thiscreates an index for the combined value of two arguments if there is noselective single-argument index. As of version 7.7 we also support indexing onthe arguments of compound terms. This feature notably improves the performancefor handling terminals in grammar rules(DCGs).

·      Unboundedinteger and rationalnumber arithmetic based on GMP library.

·      Thegoodies: modules(upward compatible to Quintus and SICStus), garbage-collection(transparentto C/C++-code, including atom and clause garbage collection), last-calloptimisation, dynamic expansion of the runtime stacks, exception-handling(includingC/C++ interface for both catching and throwing exceptions).

·      attributedvariables, coroutining(freeze/2,when/2,dif/2), global variables,cyclic terms.

·      Flag-controlledhandling of occurs-check(false/true/error) see current_prolog_flag/2. Efficient implementation of occurs checking thatonly verifies that new bindings do not introduce cycles.

·      UNICODE character set handling internal. Ideal for web andinternational applications.

·      Multi-threading support: run multiple pre-emptively scheduled prologengines on the same database.

·      Engines,also known as interactors provide coroutines that can be used forstate accumulation and massive concurrency for e.g., swarm intelligenceand simulations.

·      Delimited continuations is a powerful building block for new controlstructures and realise aspect programming.

·      Tabling(SLG resolution) provides a more robust resolution technique forsolving queries over complex interrelated rules with guaranteed termination.

1.4.2 约束处理Constraint handling

·      •CHR(约束处理规则)，clp(FD)，clp(R，Q)和其他各种库。

·      Librariesfor CHR(ConstraintHandling Rules), clp(FD),clp(R,Q)and various others.

1.4.3 连通性Connectivity

•SWI-Prolog为HTTP提供广泛的客户端和服务器库。HTTP服务器框架处理生成HTML，交换JSON或XML，身份验证，会话等等。客户端和服务器都支持HTTPS。

•灵活快速的C语言和C ++语言接口。该接口允许双向调用，双向处理非确定性以及在C/C ++项目中嵌入SWI-Prolog内核。

•可以使用JPL，Python和C＃等高级语言(如Java)的接口。

•ODBC接口提供数据库连接。

•低级网络支持包括套接字(TCP和UDP)，SSL和TIPC。

•用于解析和生成SGML/XML/HTML，JSON和YAML的库

·      SWI-Prologprovides extensive client and server libraries for HTTP. The HTTPserver framework deals with generating HTML, exchange of JSON or XML,authentication, sessions, and much more. Both client and server supports HTTPS.

·      Flexibleand fast interface to the C- and C++-language. The interfaceallows for calling both ways, handling of non-determinism both ways and embeddingof the SWI-Prolog kernel in C/C++ projects.

·      Interfacesto high level languages such as Java using JPL, Python and C# are available.

·      Databaseconnectivity is provided by the ODBCinterface.

·      Lowlevel network support includes sockets(both TCP and UDP), SSL and TIPC.

·      Librariesfor parsing and generating SGML/XML/HTML, JSONand YAML

·      LinkedData(RDF) support includes reading and writing many RDF formats(RDF/XML,Turtle, Ntriples, NQuads) and an efficient triple store. See the semwebpackage.

1.4.4 开发工具Development tools

•通过XPCE(Windows，Unix/Linux，MacOSX)支持图形的所有平台上的源级调试器。

•所有主要平台(Windows，Linux，MacOSX)的执行分析器(时间和呼叫统计)。

•交叉参考。gxref/0为可扩展的Prolog交叉引用器(外部参照)提供了图形前端。

•通过PlDoc识别编程支持。提供手册和应用程序文档的集成视图，并为您的应用程序生成LaTeX文档。

•通过PlUnit进行单元测试支持。

•SWISH提供了一个基于Web的平台，用于在协作环境中开发和运行Prolog代码。

·      Source-level debugger on all platforms that supports graphics through XPCE(Windows,Unix/Linux, MacOSX).

·      Executionprofiler(timeand call statistics) for all major platforms(Windows, Linux, MacOSX).

·      Cross-Referencer. gxref/0provides a graphical front-end for the extensible Prolog cross-referencer(xref).

·      Literateprogramming support through PlDoc.Provides integrated view on manual and application documentation and producingLaTeX documentation for your application.

·      Unittesting support through PlUnit.

·      SWISHprovides a web-based platform for developing and running Prolog code in acollaborative environment.

1.4.5 兼容性Compatibility

•全面的内置谓词集，涵盖ISO标准的第1部分，事实上的爱丁堡Prolog标准以及Quintus和SICStus Prolog的重要部分。与Ciao，YAP和GNU-Prolog的公平兼容性。虽然目标是尽可能保持兼容性，但SWI-Prolog故意偏离ISO标准以适应其他功能并与现代语言同步。阅读更多扩展和方向

·      Comprehensiveset of built-in predicates, covering Part 1 of the ISO standard, thede-facto Edinburgh Prolog standard and important parts of Quintus andSICStus Prolog. Fair compatibility to Ciao, YAP and GNU-Prolog. Although theaim is to maintain compatibility wherever possible, SWI-Prolog deliberatelydeviates from the ISO standard to accommodate additional functionality andsynchronise with modern languages. Read more in Extensionsand Directions

1.4.6 便携性Portability

•适用于许多平台，包括几乎所有Unix/Linux平台，Windows，MacOS X(使用Xquartz forgraphics)WebAssembly(WASM)，Android Termux等等。支持32位和64位硬件。SWI-Prolog已在许多CPU上进行编译和测试，例如x86，x64，SPARC，PowerPC，许多ARM型号。源是普通的C99，使用CMake自动配置(自版本7.7.20起)。对交叉编译的支持正在稳步提高。

•与机器无关的保存状态(保存在一个平台上，使用另一个平台的虚拟机运行)。

•Windows(32/64位)和MacOS X(64位)的常规二进制分发版和Ubuntu的PPA(稳定和开发)

•定期分发完整的源包。这些来源也可通过GIT获取。

·      Portable to many platforms, including almost all Unix/Linuxplatforms, Windows, MacOS X(using Xquartz for graphics) WebAssembly(WASM),Android Termux and many more. Both 32-bit and64-bit hardware is supported. SWI-Prolog has been compiled andtested on many CPUs, e.g., x86, x64, SPARC, PowerPC, many ARM models. Sourcesare plain C99, configured automatically using CMake(as of version 7.7.20).Support for cross-compilation is steadily improving.

·      Machine-independentsaved-states(save on one platform, run using the virtual machine ofanother platform).

·      Regularbinary distributions for Windows(32/64 bits) and MacOS X(64 bits)) andPPAs for Ubuntu(stableand development)

·      Regulardistribution of the full source packages. The sources are alsoaccessible through GIT.

1.4.7 法律Legal

Tag confusing pages withdoc-needs-help|Tags are associated to yourprofile if you are logged in

2. Prolog

Prolog

Prolog(Programming in Logic的缩写)是一种逻辑编程语言。它建立在逻辑学的理论基础之上，最初被运用于自然语言等研究领域。现已广泛的应用在人工智能的研究中，可以用来建造专家系统、自然语言理解、智能知识库等。同时对一些通常的应用程序的编写也很有帮助，能够比其他的语言更快速地开发程序，因为它的编程方法更像是使用逻辑的语言来描述程序。

Prolog图册

2.1 简介

Prolog(Programming in Logic的缩写)是一种逻辑编程语言。它建立在逻辑学的理论基础之上，最初被运用于自然语言等研究领域。现已广泛的应用在人工智能的研究中，可以用来建造专家系统、自然语言理解、智能知识库等。同时对一些通常的应用程序的编写也很有帮助，能够比其他的语言更快速地开发程序，因为它的编程方法更象是使用逻辑的语言来描述程序。

2.2 历史

Prolog语言最早由Aix-Marseille大学的AlainColmerauer与PhillipeRoussel等人于60年代末研究开发。1972年被公认为是Prolog语言正式诞生的年份，自1972年以后，分支出多种Prolog的方言。最主要的两种方言为Edinburgh和Aix-Marseille。最早的Prolog解释器由Roussel建造，而第一个Prolog编译器则是 DavidWarren编写的。

Prolog一直在北美和欧洲被广泛使用。日本政府曾经为了建造智能计算机而用Prolog来开发ICOT第五代计算机系统。在早期的机器智能研究领域，Prolog曾经是主要的开发工具。

80年代Borland开发的Turbo Prolog，进一步普及了Prolog的使用。1995年确定了ISOProlog标准。

2.3 特点

1. prolog程序没有特定的运行顺序，其运行顺序是由电脑决定的，而不是编程序的人。

2. prolog程序中没有if、when、case、for这样的控制流程语句

3. prolog程序和数据高度统一

4. prolog程序实际上是一个智能数据库

prolog的原理就是关系数据库，它是建立在关系数据库的基础上的。在以后的学习中你会发现它和SQL数据库查询语言有很多相似之处。使用prolog可以很方便的处理数据。

5. 强大的递归功能

2.4 语法示例

human(kate).

human(bill).

likes(kate,bill).

——表示kate和bill是人(human)，kate喜欢bill；

friend(X,Y):-likes(X,Y),likes(Y,X).

——表示对于两个对象XY，如果X喜欢Y，且Y喜欢X，那么他们是朋友。

2.5 范例

Quicksort

/*quicksort2.pl,原始来源：http://en.wikipedia.org/wiki/Prolog*/

/*quicksort()中的第二个引数带有排序好的结果　*/

/*仅为示范，若为gprolog使用者则用内建sort等较佳*/

/*gprolog下之编译，例：gplc--min-size quicksort.pl*/

/*执行 quicksort2 后会出现排序结果[2,9,18,18,25,33,66,77] */

q:-L=[33,18,2,77,66,18,9,25], last(P,_),(quicksort(L,P,_),write(P),nl)./* 加入last/2会在印P时没复合项*/

partition([],_,[],[])./* 此行表空集亦视为分割(分割成空集与空集)*/

partition([X|Xs],Pivot, Smalls, Bigs):-/* list分成SmallsBigs;rule保证Smalls<PivotBigs>=Pivot*/

(X @< Pivot ->

Smalls=[X|Rest],

partition(Xs,Pivot, Rest, Bigs)

;Bigs =[X|Rest],

partition(Xs,Pivot, Smalls, Rest)

).

quicksort([])-->[]./* empty list视为排序好的list*/

quicksort([X|Xs])-->/*此行相当于quicksort([X|Xs],Start,End) :- ruleStartsortedlist */

{partition(Xs, X, Smaller, Bigger)},/* 由上行最左端元素为Pivot */

quicksort(Smaller),[X],quicksort(Bigger)./* 此行相当于 quicksort(Smaller,Start,A),

A=[X|B],注意首字母大写者皆视为变数(list)

quicksort(Bigger,B,End).*/

:-initialization(q)./*启动qgoals*/

Sort

/*sortcsj.pl 原始参考：Computer Science J. GlennBrookshear */

/*sortcsj()中的第二个引数带有排序好的结果　*/

/* 仅为示范，若为gprolog使用者则用内建sort等较佳 */

/* gprolog下之编译，例：gplc--min-size sortcsj.pl*/

/* 执行sortcsj 后会出现排序结果 [2,9,18,18,25,33,66,77] */

q:-L=[33,18,2,77,18,66,9,25],(sortcsj(L,P),write(P),nl).

sortcsj(L,S):-permutation(L,S), ordered(S)./* L为原list, S为排序好的list, 此为permutation关系(built-in)*/

ordered([])./*empty list视为排序好的list */

ordered([_|[]])./*只有一元素之list视为排序好的list */

ordered([A|[B|T]]):-A =< B, ordered([B|T])./* rule约束所谓的排序好是指前项元素小于或等于后一项元素 */

:-initialization(q)./*启动qgoals*/

/*tstpx.pl */

/* 罗素佯谬(罗素悖论)(皇帝新脑罗杰.彭罗斯 p.120)会导致不停机(使得gprolog产生 stackoverflow) */

/* gprolog下之编译，例：gplc--min-size tstpx.pl*/

q:-px(_)./* 找寻任何可使 px() rule 成立的方式 */

px(1):-\+px(1)./* 规定此rule不成立。i.e. rule为假时此rule才为真(佯谬)*/

:-initialization(q)./*启动qgoal */

(注2：资料来自swi-prolog.org[7-8]和百度百科等[9]。词条标签：科技术语，科学。词条统计：浏览163103次，编辑21次历史版本，最近更新2018-10-07：monarch巨蟹。[9])

—END—

1. 推理，《现代汉语词典第6版》第1323页．

2. [德] 黑格尔，译者：梁志学．《逻辑学》．北京：人民出版社，2002：10-41．

3. 刘颖，苏巧玲．《医学心理学》．北京：中国华侨出版社，1997：27-28．

4. 叶清逸. 递推与递归.[EB/OL]; 网易智能, https://blog.csdn.net/u013634252/article/details/80551060, 2018-06-02, visitingdata:2019-01-26.

5. 小小小青菜. 循环 & 递归 & 递推 之间的联系和区别.[EB/OL]; https://blog.csdn.net/weixin_35909255/article/details/70738323, 2017-04-25, visitingdata:2019-01-26.

6. Never-say-Never. 编程中，循环、迭代、遍历和递归之间的区别.[EB/OL]; CSDN, https://blog.csdn.net/mad1989/article/details/8933078,2013-05-16, visiting data:2019-01-26.

7. baidujingyan. SWI-Prolog的递归.[EB/OL]; baidujingyan, https://jingyan.baidu.com/article/4f7d5712099ec41a201927ed.html,2018-08-15, visiting data:2019-01-26.

8. SWI-Prolog. Robust, mature, free. Prolog for the real world. --SWI-Prolog.[EB/OL]; SWI-Prolog, http://www.swi-prolog.org/, visitingdata:2019-01-26.

9. SWI-Prolog. SWI-Prolog's features.[EB/OL]; SWI-Prolog,http://www.swi-prolog.org/features.html, visiting data:2019-01-26.

10. 最近更新：monarch巨蟹(2018-10-07). Prolog.[EB/OL]; baidubaike,https://baike.baidu.com/item/Prolog/, 2018-10-07, visiting data:2019-01-26.

x.秦陇纪．数据简化社区Python官网Web框架概述；数据简化社区2018年全球数据库总结及18种主流数据库介绍；数据科学与大数据技术专业概论；人工智能研究现状及教育应用；信息社会的数据资源概论；纯文本数据溯源与简化之神经网络训练；大数据简化之技术体系．[EB/OL]；数据简化DataSimp(微信公众号)，http://www.datasimp.org，2017-06-06．

(附PDF发“递归prolog简介”下载)

Appx(1236字).数据简化DataSimp社区简介

LIFE

Life begins at the end of your comfortzone.——Neale Donald Walsch

THE DAY

The strength of purpose and the clarity ofyour vision，alongwith the tenacity to pursue it，is your underlying driver ofsuccess.——Ragy Tomas

• 点赞 3
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏 2
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

秦陇纪10数据简化DataSimp

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-24
06-23 1万+

07-14 6331
12-31 1790
04-03 139
10-17 1万+
11-25 394
10-06 1443
12-10 1334