Letter frequency: Difference between revisions

Add Refal
(added RPL)
(Add Refal)
 
(6 intermediate revisions by 3 users not shown)
Line 2,984:
Q 378
</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun tally-letter-frequency-in-file ()
"Open a file and count the number of times each letter appears."
(let ((alphabet "abcdefghijklmnopqrstuvwxyz") ; variable to hold letters we will be counting
(current-letter) ; variable to hold current letter we will be counting
(count) ; variable to count how many times current letter appears
(case-fold-search t)) ; ignores case
(find-file "~/Documents/Elisp/MobyDick.txt") ; open file in a buffer (or switch to buffer if file is already open)
(while (>= (length alphabet) 1) ; as long as there is at least 1 letter left in alphabet
(beginning-of-buffer) ; go to the beginning of the buffer
(setq current-letter (substring alphabet 0 1)) ; set current-letter to first letter of alphabet
(setq count (how-many current-letter)) ; count how many of this letter in file
(end-of-buffer) ; go to the end of the buffer
(insert (format "\n%s%s - %7d" current-letter (upcase current-letter) count)) ; write how many times that letter appears
(setq alphabet (substring alphabet 1 nil))) ; remove first letter from alphabet
(insert "\n")))
 
</syntaxhighlight>
{{out}}
<pre>
 
aA - 79220
bB - 17203
cC - 23318
dD - 38834
eE - 119345
fF - 21252
gG - 21287
hH - 63769
iI - 66671
jJ - 1176
kK - 8228
lL - 43349
mM - 23626
nN - 66778
oO - 70808
pP - 17873
qQ - 1581
rR - 53589
sS - 65136
tT - 89874
uU - 27205
vV - 8724
wW - 22556
xX - 1064
yY - 17242
zZ - 635
</pre>
 
 
=={{header|Erlang}}==
Line 3,608 ⟶ 3,659:
=={{header|Haskell}}==
Short version:
<syntaxhighlight lang="haskell">import Data.List (group, sort)
 
import Control.Arrow ((&&&))
main :: IO ()
main = interact (show . map (head &&& length) . group . sort)</syntaxhighlight>
main = interact (show . fmap ((,) . head <*> length) . group . sort</syntaxhighlight>
 
or, as an alternative to sorting and grouping the whole string, we could use some kind of container as the accumulator for a single fold, for example:
Line 4,675 ⟶ 4,727:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .countLetters = ffn(.s) {
for[=h{}] .s2 in split(replace(.s, RE/\P{L}/)) {
_for[.s2; 0] += 1
Line 4,682 ⟶ 4,734:
 
val .counts = .countLetters(readfile "./fuzz.txt")
writeln join "\n", map ffn(.k) $"\.k;: \.counts[.k];", keys .counts</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 6,522 ⟶ 6,575:
$all_data count_letters</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, <Arg 1>: {
= <Prout 'No filename given'>;
e.File, <ReadFile 1 e.File>: e.Text,
<Tally e.Text>: e.Counts
= <ShowLetterCounts (e.Counts) <Letters>>;
};
};
 
Letters {
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
};
 
ShowLetterCounts {
(e.T) = ;
(e.T) s.L e.Ls,
<Upper s.L>: s.UL, <Item (e.T) s.UL>: s.ULN,
<Lower s.L>: s.LL, <Item (e.T) s.LL>: s.LLN,
<+ s.ULN s.LLN>: s.Total
= <Prout s.UL s.LL ': ' <Symb s.Total>>
<ShowLetterCounts (e.T) e.Ls>;
};
 
ReadFile {
s.Chan e.Filename =
<Open 'r' s.Chan e.Filename>
<ReadFile (s.Chan)>;
(s.Chan), <Get s.Chan>: {
0 = <Close s.Chan>;
e.Line = e.Line '\n' <ReadFile (s.Chan)>;
};
};
 
Tally {
(e.T) = e.T;
(e.T) s.X e.Xs = <Tally (<Inc (e.T) s.X>) e.Xs>;
e.Xs = <Tally () e.Xs>;
}
 
Inc {
(e.1 (s.I s.N) e.2) s.I = e.1 (s.I <+ 1 s.N>) e.2;
(e.X) s.I = e.X (s.I 1);
};
 
Item {
(e.1 (s.I s.N) e.2) s.I = s.N;
(e.X) s.I = 0;
};</syntaxhighlight>
{{out}}
The result of running the program on its own source file:
<pre>Aa: 22
Bb: 2
Cc: 16
Dd: 5
Ee: 75
Ff: 10
Gg: 5
Hh: 11
Ii: 26
Jj: 1
Kk: 1
Ll: 49
Mm: 8
Nn: 33
Oo: 18
Pp: 6
Qq: 1
Rr: 17
Ss: 55
Tt: 44
Uu: 14
Vv: 2
Ww: 5
Xx: 12
Yy: 7
Zz: 1</pre>
=={{header|REXX}}==
===version 1===
2,098

edits