⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08ex03.pro

📁 prolog,人工智能推理程序,运行环境prolog
💻 PRO
字号:
/*
   Turbo Prolog 2.0 Chapter 8, Example Program 3
   
   Copyright (c) 1986, 88 by Borland International, Inc
   
*/
   
domains
   list = integer* /* or whatever type you want to use */

predicates
   length_of(list, integer, integer)

clauses
/* * * * * * * * * * * * * * * * * * * * * * *
 * If the list is empty, bind the second arg *
 *   (the result) to the third (the counter).*
 * * * * * * * * * * * * * * * * * * * * * * */
   length_of([], Result, Result).

/* * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Otherwise, add 1 to counter and recurse, binding  *
 * Result in this invocation to Result in the next.  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * */
   length_of([_|T], Result, Counter) :-
      NewCounter = Counter + 1,
      length_of(T, Result, NewCounter).

goal
   length_of([1, 2, 3], L, 0), /* start with Counter = 0 */
   write(L), nl.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -