• Re: Cells compared to Flow-Based Programming

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Fri Aug 8 18:15:57 2025
    From Newsgroup: comp.lang.lisp

    Frank Buss wrote:

    For example, take a look at the telegram problem:

    http://en.wikipedia.org/wiki/Flow-based_programming#.22Telegram_Problem.22

    Your task is to write a function, which reformats a text for a specified
    line width, e.g. this call:

    (telegram "
    Du bist am Ende- was du bist. Setz dir Pern++cken auf von Millionen Locken, Setz deinen Fun++ auf ellenhohe Socken, Du bleibst doch immer, was du bist." 30)

    outputs these lines:

    Du bist am Ende- was du bist.
    Setz dir Pern++cken auf von
    Millionen Locken, Setz deinen
    Fun++ auf ellenhohe Socken, Du
    bleibst doch immer, was du
    bist.


    Gauche Scheme

    (use srfi-13) ;; string-tokenize
    (use srfi-197) ;; chain

    (define (string->words str)
    (string-tokenize str))

    (define (words->lines words width)
    (let ((output '())
    (tmp (if (null? words) "" (pop! words))))
    (while (pair? words)
    (let ((word (pop! words)))
    (if (< (+ (string-length word) (string-length tmp)) width)
    (set! tmp (string-append tmp " " word))
    (begin
    (push! output tmp)
    (set! tmp word)))))
    (when (not (equal? "" tmp)) (push! output tmp))
    (reverse output)))

    (define (telegram str width)
    (chain
    str
    (string->words _)
    (words->lines _ width)))

    (for-each
    print
    (telegram "
    Du bist am Ende- was du bist. Setz dir Pern++cken auf
    von Millionen Locken,
    Setz deinen Fun++ auf ellenhohe Socken, Du bleibst doch
    immer, was du bist."
    30))

    Du bist am Ende- was du bist.
    Setz dir Pern++cken auf von
    Millionen Locken, Setz deinen
    Fun++ auf ellenhohe Socken, Du
    bleibst doch immer, was du
    bist.
    --
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2