How to resolve the algorithm Letter frequency step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Letter frequency step by step in the Common Lisp programming language

Table of Contents

Problem Statement

Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Letter frequency step by step in the Common Lisp programming language

Source code in the common programming language

(defun letter-freq (file)
  (with-open-file (stream file)
    (let ((str (make-string (file-length stream)))
	  (arr (make-array 256 :element-type 'integer :initial-element 0)))
      (read-sequence str stream)
      (loop for c across str do (incf (aref arr (char-code c))))
      (loop for c from 32 to 126 for i from 1 do
	    (format t "~c: ~d~a"
		    (code-char c) (aref arr c)
		    (if (zerop (rem i 8)) #\newline #\tab))))))

(letter-freq "test.lisp")


  

You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Fraction reduction step by step in the Phix programming language
You may also check:How to resolve the algorithm Delegates step by step in the Aime programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the JavaScript programming language