Language in Practice — Clean Language
← All tutorials
language-deep-dive10 min

Language in Practice

Let's build a word-frequency counter — a program that tokenizes a sentence, counts how many times each word appears, and reports the most frequent ones. It uses most of what this track covered and shows how the language features work together.

Helper functions handle the parsing and counting logic:

functions:\n    list<string> tokenize(string text)\n        string lower = text.toLowerCase()\n        list<string> raw = lower.split(" ")\n        list<string> words = []\n        iterate word in raw\n            string clean = word.trim()\n            if clean.length() > 0\n                words.add(clean)\n        return words\n\n    integer count_word(list<string> words, string target)\n        integer count = 0\n        iterate word in words\n            if word == target\n                count = count + 1\n        return count

tokenize() normalizes to lowercase, splits by space, and filters empty strings. count_word() iterates the whole list counting matches. Both functions are focused: they do one thing and return a typed result. Keeping functions small makes them easy to test and reuse.

The main program ties the helpers together:

start:\n    string text = "the quick brown fox jumps over the lazy dog the fox"\n    list<string> words = tokenize(text)\n    print("Total words: " + words.length().toString())\n\n    list<string> seen = []\n    iterate word in words\n        if seen.contains(word) == false\n            seen.add(word)\n            integer freq = count_word(words, word)\n            if freq > 1\n                print(word + ": " + freq.toString() + " times")
Total words: 10\nthe: 3 times\nfox: 2 times

The seen list tracks which words have already been reported, so each word is counted exactly once. .contains() returns boolean directly to the if condition. The pattern — maintain a seen list, skip if already processed — is a general technique for deduplication.

Quick recap

  • Split programs into focused functions — each does one thing and returns a typed result
  • Use a seen list to track which items have already been processed
  • List methods (.contains, .add, .length) and string methods (.split, .trim, .toLowerCase) compose naturally
  • The same patterns — accumulate, search, deduplicate — appear in programs of any size
Copied!