• Re: Reasons for preferring Lisp, and for what

    From B. Pym@21:1/5 to Joel Ray Holveck on Mon Aug 5 05:38:04 2024
    Joel Ray Holveck wrote:

    As a concrete example, suppose (as I did in a program I recently
    wrote) that you have a string with several arbitrary variable names
    you need to substitute in, and a hash with their values. This can be
    done in one line in Perl:
    # $string holds something like:
    # 'The $dimension of the $obj is $length cm.'
    # %vars holds something like:
    # { $dimension => length, $obj => plank, $length => 105 }
    $string =~ s/\$(\S+)/$vars->{$1}/g;

    Now, look at a Lisp parallel. You have a list, and you want to
    substitute keywords with their values from an alist:
    ;; LIST holds something like:
    ;; (the :dimension of the :object is :value cm)
    ;; VARS holds something like:
    ;; ((:dimension . length) (:object . plank) (:value . 105))
    (mapcar #'(lambda (elt)
    (if (keywordp elt)
    (cdr (assoc elt vars))
    elt))
    list)

    newLISP

    (define str "The $dimension of the $obj is $length cm.")
    (define table '((dimension "length") (obj "plank") (length "105")))

    (replace "\\$(\\w+)" str (lookup (sym $1) table) 0)

    "The length of the plank is 105 cm."

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