Click below to go directly to a specific section:
This program demonstrates how to count letters in a sentence and compares Smalltalk with Pascal
Pascal | Smalltalk |
program frequency | |
const | |
size = 80; | |
var | |
s: string[size]; | |scfk| |
i: integer; | |
c: char; | |
f: array[1..26] of integer; | f := Array new: 26. |
k: integer; | |
begin | |
writeln('enter line'); | s := Prompter prompt: 'Enter line' |
readln(s); | default: ''. |
for i := 1 to 26 do | 1 to: 26 do [:i | f at: i put: 0]. |
f[i] := 0; | |
for i := 1 to size do | 1 to: s size do: [:i | |
begin | |
c : = | c := (s at: i) asLowerCase. |
asLowerCase(s[i}); | |
if isLetter(c) then | c isLetter |
begin | ifTrue: [ |
K := ord(c) - ord('a') + 1; | k := c asciiValue - &a asciiValue + 1 |
f[k] := f[k] + 1 | |
end | ] |
end | ]. |
for i := 1 to 26 do | ^f |
write(f[i], ' ') | |
end. | |
Using Smalltalks powerful built-in buiding blocks the same code could have been written as this:
| s f |
s := Prompter prompt: 'Enter line' default: ''.
f := Bag new.
s do [ :c | c isLetter ifTrue: [f add: c asLowerCase}}.
^f
This program was created using Smalltalk Express for Windows.
Last modified: 03:37 PM on 10/01/1999