On 2012-02-18, WJ <
w_a_...@yahoo.com> wrote:
>
neilharp...@googlemail.com wrote:
>
>> I am learning lisp as my first language. I am reading through
>> Practical common lisp.
>>
>> This is the block thats confusing me:
>>
>> (defun dump-db ()
>> (dolist (cd db)
>> (format t "~{~a:~10t~a~%~}~%" cd)))
>>
>> dolist loops through the list db with the 'cd' variable right?
>>
>> '~a' prints the list in a more readable format. but what about ~{, I
>> dont get what thats doing.
>>
>> Thanks.
>
> The function should not be operating on a global variable.
> And the format statement is too obfuscated.
According to the author of Ruby, it is the "Perlification" of Lisp.
That is a near synonym for "obfuscation".
Your program is four times as long and has about the the obfuscation.
You're just diluting the obfuscation with verbiage.
The format is not hard to understand. ~{ ... ~} denotes repetition
over the list, ~a is argument substitution, ~10t means tab to the 10th
column, and ~% is newline.
> MatzLisp:
>
> $db =
> [[:TITLE, "Home", :ARTIST, "Dixie Chicks", :RATING, 9, :RIPPED, true],
> [:TITLE, "Fly", :ARTIST, "Dixie Chicks", :RATING, 8, :RIPPED, true],
> [:TITLE, "Roses", :ARTIST, "Kathy Mattea", :RATING, 7, :RIPPED, true]]
Obfuscated. Why all the commas, and why can't we just assign to "db"
rather than "$db"? What does the sygil achieve? There does not appear
to be any ambiguity that it solves over just
db = [[...]]
> def dump_db( db )
> db.each{|cd|
Why no $ sygil on this db?
> cd.each_slice(2){|k,v|
> print "#{ k }:".ljust( 10 )
> puts v }
> puts }
> end
This is longer and more complicated than
(defun dump-db (db)
(dolist (cd db)
(format t "~{~a:~10t~a~%~}~%" cd)))
Furthermore, the format solution could be shortened more, since another level
of ~{ .. ~} will replace the dolist. We end up with this nice one-liner:
(format t "~{~{~a:~10t~a~%~}~}" db)
I will take this over a "Fortranification of Logo".
And in case you're dumb enough to keep your CD database in this Ruby jail,
here is how you can bust it out.
It's easier in TXR to parse the raw data out of your Ruby source code
than for Ruby to work with the objects "natively".
$ cat cd.txr
@(collect)
@(coll):@prop, @(cases)"@val"@(or)@val@/[\], ]/@(end)@(end)
@(end)
@(output)
@ (repeat)
@ (repeat)
@{`@prop:` 10} @val
@ (end)
@ (end)
@(end)
$ txr cd.txr cd.rby
TITLE: Home
ARTIST: Dixie Chicks
RATING: 9
RIPPED: true
TITLE: Fly
ARTIST: Dixie Chicks
RATING: 8
RIPPED: true
TITLE: Roses
ARTIST: Kathy Mattea
RATING: 7
RIPPED: true
$ cat cd.rby