Sujet : Re: Reasons for preferring Lisp, and for what
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 05. Aug 2024, 06:38:04
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v8pofo$h4dr$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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."