Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.misc > #11833

Re: Resources to learn common lisp?

From ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups comp.lang.misc
Subject Re: Resources to learn common lisp?
Date 2026-08-27 10:47 +0000
Organization Stefan Ram
Message-ID <expressiveness-20260827113139@ram.dialup.fu-berlin.de> (permalink)
References (18 earlier) <87tsojm54k.fsf@nightsong.com> <116jmt4$3jrlq$1@dont-email.me> <87fr01mplp.fsf@nightsong.com> <87tsogiuxx.fsf@safunu.org> <expressiveness-20260827111602@ram.dialup.fu-berlin.de>

Show all headers | View raw


ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
>According to secondary sources, Felleisen deems two programming
>languages to be equally expressive if programs can be rewritten
>into the other language, maybe using local macros (because they
>are just "syntactic suger"). 

  The following Clojure program is an example of what the
  chatbot deems to be expressive (untested):

(fn [file-path]
 (->> 
  (slurp file-path)               ; Read entire file into a string
  (re-seq #"\w+")                 ; Split text into a sequence of words
  (map clojure.string/lower-case) ; Convert all words to lowercase
  (filter #(>= (count %) 5))      ; Keep only words with 5+ characters
  (frequencies)                   ; Count occurrences 
  (sort-by val >)))               ; Sort the results by count, desc.

  This is intended to solve this problem: 
  - read a text file,
  - filter out words shorter than 5 letters, 
  - count the frequencies of the remaining words, and 
  - sort them by their counts.

  If I understand Felleisen 1991 correctly from my secondary sources,
  a programming language where you can write that program in the same
  fashion (maybe using macros) has equal expressiveness. But when you
  have to change the structure from this linear list to something more
  complicated in your language, you have less expressiveness.

  Here's the Python version (untested):

import re
from collections import Counter
from functools import reduce

def pipe(data, *functions):
    """Passes data sequentially through a chain of functions."""
    return reduce(lambda val, func: func(val), functions, data)

def count_words(file_path: str):
    return pipe(
        open(file_path, 'r').read(),
        lambda text: re.findall(r'\w+', text),
        lambda words: (w.lower() for w in words),
        lambda words: (w for w in words if len(w) >= 5),
        Counter,
        lambda counts: counts.most_common()
    )

  You could say that "pipe" is that macro that allows us to
  emulate Clojure, but to Felleisen it might be a "global"
  macro and, therefore, not be allowed in this context.

  C++ can do such a linear sequence, but it's much more verbose,
  distracting the programmer from the clean flow (untested):

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ranges>
#include <algorithm>
#include <map>

auto count_words(const std::string& file_path) {
    std::ifstream file(file_path);
    std::map<std::string, int> word_counts;
    std::string word;

    // Helper to sanitize a word to lowercase alphanumeric characters
    auto sanitize = [](std::string w) {
        auto clean = w | std::views::filter([](char c) { return std::isalnum(c); })
                       | std::views::transform([](char c) { return std::tolower(c); });
        return std::string(clean.begin(), clean.end());
    };

    // 1. Stream data, sanitize, and filter by length dynamically
    auto word_stream = std::views::istream<std::string>(file)
                     | std::views::transform(sanitize)
                     | std::views::filter([](const std::string& w) { return w.length() >= 5; });

    for (const auto& w : word_stream) { word_counts[w]++; }

    // 2. Sort the map by value using C++23 range actions
    std::vector<std::pair<std::string, int>> sorted(word_counts.begin(), word_counts.end());
    std::ranges::sort(sorted, std::ranges::greater{}, &std::pair<std::string, int>::second);

    return sorted;
}

  However, we need to take into account what benefits the
  verbosity brings. A lot of the verbosity of the C++ solution
  might be due to namespaces like "std::", but they might
  help to organize large programs.

  Common Lisp can implements Clojure's "->>" as a macro (untested).
  If Felleisen deems this to be a "global macro" it would not
  be allowed in this context.

;; 1. Define Clojure's threading macro in pure Common Lisp
(defmacro ->> (initial-value &rest forms)
  "Threads the initial-value through the forms as the last argument of each."
  (reduce (lambda (acc form)
            (if (listp form)
                `(,(car form) ,@(cdr form) ,acc)
                `(,form ,acc)))
          forms
          :initial-value initial-value))

;; 2. The expressive word-count function
(defun count-words (file-path)
  (->> (uiop:read-file-string file-path)          ; Read file to string (UIOP is standard in modern CL)
       (cl-ppcre:split "[^a-zA-Z0-9]+")           ; Split into words using regex
       (remove-if (lambda (w) (string= w "")))    ; Filter out empty strings from split
       (mapcar #'string-downcase)                 ; Normalize to lowercase
       (remove-if (lambda (w) (< (length w) 5)))  ; Keep only words with 5+ characters
       (reduce (lambda (hash word)                ; Count frequencies into a hash table
                 (incf (gethash word hash 0))
                 hash)
               _ :initial-value (make-hash-table :test 'equal))
       (alexandria:hash-table-alist)              ; Convert to an association list (word . count)
       (sort _ #'> :key #'cdr)))                  ; Sort descending by the count



Back to comp.lang.misc | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Resources to learn common lisp? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-24 19:38 +0800
  Re: Resources to learn common lisp? tfb <tfb@work.it.out> - 2026-08-24 12:22 +0000
    Re: Resources to learn common lisp? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-24 21:04 +0800
      Re: Resources to learn common lisp? tfb <tfb@work.it.out> - 2026-08-24 13:18 +0000
        Re: Resources to learn common lisp? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-24 22:16 +0800
          Re: Resources to learn common lisp? tfb <tfb@work.it.out> - 2026-08-24 20:10 +0000
            Re: Resources to learn common lisp? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-25 10:34 +0800
        Re: Resources to learn common lisp? Paul Rubin <no.email@nospam.invalid> - 2026-08-24 17:10 -0700
          Re: Resources to learn common lisp? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-08-25 10:41 +0800
          Re: Resources to learn common lisp? tfb <tfb@work.it.out> - 2026-08-25 09:22 +0000
            Re: Resources to learn common lisp? Paul Rubin <no.email@nospam.invalid> - 2026-08-25 22:13 -0700
              Re: Resources to learn common lisp? Anton Antimo <anton@safunu.org> - 2026-08-26 15:43 -0300
                Re: Resources to learn common lisp? Paul Rubin <no.email@nospam.invalid> - 2026-08-26 18:23 -0700
                Re: Resources to learn common lisp? Anton Antimo <anton@safunu.org> - 2026-08-28 17:29 -0300
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-08-27 10:17 +0000
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-08-27 10:47 +0000
                Re: Resources to learn common lisp? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-18 10:49 +0800
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-18 10:25 +0000
                Re: Resources to learn common lisp? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-18 11:05 +0800
                Re: Resources to learn common lisp? red floyd <no.spam.here@its.invalid> - 2026-09-17 21:43 -0700
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-18 10:28 +0000
                Re: Resources to learn common lisp? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-18 21:33 +0800
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-18 13:51 +0000
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-18 14:23 +0000
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-18 15:18 +0000
                Re: Resources to learn common lisp? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-19 00:07 +0800
                Resources to learn Japanese (was: Resources to learn common lisp?) ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-18 16:52 +0000
                Re: Resources to learn common lisp? boltar@caprica.universe - 2026-09-18 15:11 +0000
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-18 15:25 +0000
                Re: Resources to learn common lisp? boltar@caprica.universe - 2026-09-18 15:49 +0000
                Re: Resources to learn common lisp? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-18 16:05 +0000
                Re: Resources to learn common lisp? boltar@caprica.universe - 2026-09-19 08:29 +0000
                Re: Resources to learn common lisp? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-19 16:50 +0800
                Re: Resources to learn common lisp? David Brown <david.brown@hesbynett.no> - 2026-09-18 18:18 +0200
                Re: Resources to learn common lisp? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-19 00:35 +0800
                Why no string switch? (was: Resources to learn common lisp?) ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-19 12:46 +0000
                Re: Why no string switch? (was: Resources to learn common lisp?) boltar@caprica.universe - 2026-09-19 14:47 +0000
                Re: Resources to learn common lisp? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-18 23:39 +0800

csiph-web