• .Re: Exercises, chap. 3, Graham

    From Robert L.@21:1/5 to Alan Crowe on Thu Feb 24 15:00:40 2022
    Alan Crowe wrote:

    On the other hand I am careful to locate the style in a
    mythical past. There is an unhappy history of the
    style being taught as the one true way to program in Lisp,
    even though it is a rather clumsy style for day
    to day programming. For example, all my routines get reduced
    to one liners by using CL's built-in functions.

    (defun trad-count-CL (symbol start list)
    (declare (ignore start))
    (cons symbol (count symbol list)))

    (defun find-types-CL(tokens types)
    (declare (ignore types))
    (remove-duplicates tokens))

    (defun multi-count-CL (types tokens)
    (loop for x in types collect (trad-count-CL x 0 tokens)))

    (defun occurrences-CL (list)
    (sort (multi-count-CL (find-types-CL list '())
    list)
    #'> :key #'cdr))

    leading to

    (defun occurrences-CL-condensed (list)
    (sort (loop for x in (remove-duplicates list)
    collect (cons x (count x list)))
    #'> :key #'cdr))

    Gauche Scheme:

    "We don't need no stinkin' loops!"

    (define (group-them stuff)
    (if (null? stuff)
    '()
    (receive (these those) (partition (cut eqv? <> (car stuff)) stuff)
    (cons these (group-them those)))))

    (define (occurrences stuff)
    (define groups (group-them stuff))
    (values
    (sort (map (lambda (xs) (cons (car xs) (length xs))) groups) > cdr)
    groups))

    (occurrences '(b a d c d c b d c d))
    ===>
    ((d . 4) (c . 3) (b . 2) (a . 1))
    ((b b) (a) (d d d d) (c c c))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)