Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.lisp
Re: Reasons for preferring Lisp, and for what
William James
3/5/2016 5:37:00 AM
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)
MatzLisp (Ruby):
vars = Hash[*%w(dimension length obj plank length 105)]
'The $dimension of the $obj is $length cm.'.gsub(/\$\w+/){|s| vars[s[1..-1]]}
===>
"The length of the plank is 105 cm."
--
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands.... [T]he real aim stays the
same: the biggest genocide in human history.... --- Nick Griffin
(
https://www.youtube.com/watch?v=K...
)
1 Answer
William James
3/5/2016 8:35:00 PM
0
WJ wrote:
> 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)
>
> MatzLisp (Ruby):
>
> vars = Hash[*%w(dimension length obj plank length 105)]
>
> 'The $dimension of the $obj is $length cm.'.gsub(/\$\w+/){|s| vars[s[1..-1]]}
> ===>
> "The length of the plank is 105 cm."
OCaml:
let table =
["$dimension", "length"; "$obj", "plank"; "$length", "105"] ;;
Str.global_substitute
(Str.regexp "\\$[a-z]+")
(fun s ->
let x = Str.matched_string s in
try List.assoc x table with Not_found -> x)
"The $dimension of the ($dummy) $obj is $length cm." ;;
===>
"The length of the ($dummy) plank is 105 cm."
--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Reasons for preferring Lisp, and for what
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password