LogicJS为JavaScript增加逻辑编程,众所周知的逻辑编程语言是Prolog。
安装
下载logic.js文件并将其移动到您的项目。
介绍
var or = logic.or, and = logic.and, eq = logic.eq, run = logic.run, lvar = logic.lvar, between = logic.between //creates two unique logic variables var x = lvar(), y = lvar() //creates a 'goal' g1 = or( and(eq(x,2), eq(y,3)), and(eq(x,y), eq(y,'dog')) ) //runs goal asking for the possible values of x and y run(g1, x) //[2, 'dog'] run(g1, y) //[3, 'dog'] run(g1, [x,y]) //[ [2, 3], ['dog', 'dog'] ] //a goal is a sequence of assertions //here, we assert that x is a value from 1 to 3 //and that y is either 1 or 2 g2 = and( between(1,3,x), or(eq(1,y), eq(2,y)) ) //get only the first 2 answers run(g2, [x,y], 2) //[ [1, 1], [1, 2] ] //get all answers run(g2, [x,y]) //[ [1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 2] ]
评论