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

在 《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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值