Jump to content

Letter frequency: Difference between revisions

completely RNRS Scheme with the exception of format, uses native Scheme character representations, more canonical indentation, and an association list instead of a vector
(completely RNRS Scheme with the exception of format, uses native Scheme character representations, more canonical indentation, and an association list instead of a vector)
Line 2,277:
=={{header|Scheme}}==
 
Using guile scheme 2.0.11, but the functions that don't use format should work in any implementation.
=== Imperative version ===
 
Note that this prints the scheme representations of characters in no particular order.
{{novice example|scheme}}
 
<lang scheme>(use-modules (ice-9 format))
For brevity, outputs only lower-case letters.
 
(define (show-char-freq let-numport table)
<lang scheme>#!/usr/local/bin/gosh
(if
(eof-object? (peek-char port))
table
(char-freq port (add-char (read-char port) table))))
 
(define (countadd-char-freqs char table)
(use srfi-1) ;; iota
(cond
((null? table) (list (list char 1)))
((eq? (caar table) char) (cons (list char (+ (cadar table) 1)) (cdr table)))
(#t (cons (car table) (add-char char (cdr table))))))
 
(define *freqs* (makeformat-vector 256table 0)table)
(if
(not (null? table))
(begin
(format #t "~10s~10d~%" (caar table) (cadar table))
(format-table (cdr table)))))
 
(define (mainprint-freq argsfilename)
(format-table (char-freq (withopen-input-from-file "../word-list.txt"filename) count-char-freqs'())))
(show-char-freqs #\a #\z))
 
(print-freq "letter-frequency.scm")</lang>
(define (count-char-freqs)
(let* ((ln (read-line))
(at-eof (eof-object? ln)))
(if (not at-eof)
(let ((string-chars (string->list ln)))
(for-each count-char-freq string-chars)
(count-char-freqs)))))
 
Output when reading own source:
(define (count-char-freq ch)
x#\( 617 48
(if (char-alphabetic? ch)
#\u 6489 6
(let* ((char-num (char->integer (char-downcase ch)))
#\s (frq (vector-ref *freqs* char-num)))9
#\e (vector-set! *freqs* char-num (+ 1 frq)))))51
w#\- 1968 19
 
v#\m 1902 9
(define (show-char-freqs first-letter last-letter)
y#\o 3633 17
(format #t "Letter Frequency~%")
#\d 19
(let* ((first-num (char->integer first-letter))
#\l (last-num (char->integer last-letter)) 30
#\space 97
(num-count (+ 1 (- last-num first-num)))
#\i (nums-list (iota num-count first-num))) 17
#\c (for-each show-char-freq nums-list))) 28
#\9 1
 
#\f 21
(define (show-char-freq let-num)
#\r 13436 40
(let ((ch (integer->char let-num))
#\a (frq (vector-ref *freqs* let-num))) 51
#\t (format #t "~6a ~8a~%" ch frq)))</lang>39
#\) 48
 
#\newline 25
Example output:
#\n 18
 
#\h 13
Letter frequency
a#\q 16421 7
b#\p 4115 9
c#\b 8216 20
d#\j 5799 1
e#\? 20144 4
f#\k 2662 1
g#\1 4129 4
h#\+ 5208 1
i#\# 13980 2
j#\g 430 1
k#\" 1925 4
l#\~ 10061 3
m#\0 5828 2
n#\% 12097 1
o#\' 12738 1
p#\y 5516 1
q#\. 378 1
r 13436
s 10210
t 12836
u 6489
v 1902
w 1968
x 617
y 3633
z 433
 
=={{header|Seed7}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.