如何理解JavaScript中的原编程 和 函数式编程

来源:互联网 发布:网络推广数据指标体系 编辑:程序博客网 时间:2024/05/16 05:21

在 《Paradigms of Artificial Intelligence Programming》一书中有一个简单的随机句子生产器程序,原网址今天好像不能打开

不过在http://blog.csdn.net/zzljlu/article/details/7830259 中的末尾有实现 为了了解JS 的函数式编程和原编程威力

也用JS实现了一下, 没有注释 望谅解

---------------------------------------------------------------------------------------------------------

var grammer;var simpleGrammar = [['sentence', '_', ['nounPhrase', 'verbPhrase']], ['nounPhrase', '_', ['article', 'noun']], ['verbPhrase', '_', ['verb', 'nounPhrase']], ['article', '_', 'the', 'a'], ['noun', '_', 'man', 'woman', 'ball', 'table'], ['verb', '_', 'hit', 'took', 'saw', 'liked']];var biggerGrammar = [['sentence', '_', ['nounPhrase', 'verbPhrase']], ['nounPhrase', '_', ['article', 'adj1', 'noun', 'pp1'], ['name'], ['pronoun']], ['verbPhrase', '_', ['verb', 'nounPhrase', 'pp1']], ['pp1', '_', null, ['pp', 'pp1']], ['adj1', '_', null, ['adj', 'adj1']], ['pp', '_', ['prep', 'nounPhrase']], ['prep', '_', 'to', 'in', 'by', 'with', 'on'], ['adj', '_', 'big', 'little', 'blue', 'green', 'adiabatic'], ['article', '_', 'the', 'a'], ['name', '_', 'pat', 'kim', 'lee', 'terry', 'robin'], ['noun', '_', 'man', 'ball', 'woman', 'table'], ['verb', '_', 'hit', 'took', 'saw', 'liked'], ['pronoun', '_', 'the', 'she', 'it', 'these', 'those', 'that']];function ruleLeft(rule) {    return (rule instanceof Array) && rule[0];};function ruleRight(rule) {    return (rule instanceof Array) && rule.slice(2);};function rewrites(category) {    return ruleRight(assoc(category, grammer));};function generate(phrase) {    if ((phrase instanceof Array)) {        return mappend(generate, phrase);    } else if (rewrites(phrase)) {        return generate(randomElt(rewrites(phrase)));    } else {        return [phrase];    };};function assoc(category, grammer) {    if ((grammer instanceof Array)) {        for (var len = grammer.length, i = 0; i < len; i = ++i) {            var rule = grammer[i];            if (category === rule[0]) {                return rule;            };        };    };};function mappend(fn, array) {    return array.map(fn).reduce(function (x, y) {        return x.concat(y);    });};function randomElt(array) {    return array[Math.floor(Math.random() * array.length)];};function generateTree(phrase) {    if ((phrase instanceof Array)) {        return phrase.map(generateTree);    } else if (rewrites(phrase)) {        var temp = generateTree(randomElt(rewrites(phrase)));        temp.unshift(phrase);        return temp;    } else {        return [phrase];    };};function generateAll(phrase) {    if ((phrase instanceof Array) && phrase.length === 0) {        return [[]];    } else if ((phrase instanceof Array)) {        return combineAll(generateAll(phrase[0]), generateAll(phrase.slice(1)));    } else if (rewrites(phrase)) {        return mappend(generateAll, rewrites(phrase));    } else {        return [[phrase]];    };};function combineAll(xarray, yarray) {    return mappend(function (y) {        return xarray.map(function (x) {            return x.concat(y);        });    }, yarray);};grammer = simpleGrammar;console.log(generateAll('sentence').length);

0 0
原创粉丝点击