RosettaCodeData/Task/Letter-frequency/Lambdatalk/letter-frequency.lambdatalk
2023-07-01 13:44:08 -04:00

50 lines
2 KiB
Text

{script
// W.frequency is added to the lambdatalk dictionary via the {script ...} special form
LAMBDATALK.DICT['W.frequency'] = function() {
// 1) simply copied from the rosetta.org #Javascript entry
var frequency = function(txt) {
var cs = txt.split(''),
i = cs.length,
dct = {},
c = '';
while (i--) {
c = cs[i];
dct[c] = (dct[c] || 0) + 1;
}
var keys = Object.keys(dct);
keys.sort();
return keys.map(function (c) { return [c, dct[c]]; });
};
// 2) then interfaced with lambdatalk
var args = arguments[0].trim().replace( /\s+/g, "␣" );
var output = frequency( args );
for (var a=[], b=[], i=0; i< output.length; i++) {
a.push( output[i][0] );
b.push( output[i][1] );
}
var pair = "{cons {A.new " + a.join(" ") +
"} {A.new " + b.join(" ") + "}}"
return LAMBDATALK.eval_forms( pair );
};
}
{def S3
Not all that Mrs. Bennet, however, with the assistance of her five daughters, could ask on the subject, was sufficient to draw from her husband any satisfactory description of Mr. Bingley. They attacked him in various ways--with barefaced questions, ingenious suppositions, and distant surmises; but he eluded the skill of them all, and they were at last obliged to accept the second-hand intelligence of their neighbour, Lady Lucas. Her report was highly favourable. Sir William had been delighted with him. He was quite young, wonderfully handsome, extremely agreeable, and, to crown the whole, he meant to be at the next assembly with a large party. Nothing could be more delightful! To be fond of dancing was a certain step towards falling in love; and very lively hopes of Mr. Bingley's heart were entertained.
}
-> S3
{def S3.freq {W.frequency {S3}}}
-> S3.freq
characters: {car {S3.freq}}
-> [!,',,,-,.,;,B,H,L,M,N,S,T,W,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,␣]
frequencies: {cdr {S3.freq}}
-> [1,1,13,3,9,2,3,2,2,3,2,1,2,1,53,13,17,29,82,17,16,36,44,1,3,34,11,41,40,8,2,35,39,55,20,7,17,2,16,132]
}