Re: sanity check (coding style)




Sylvain wrote:
> Christophe Rhodes wrote:
> > I'd probably write
> > (defun digest2string (v)
> > (format nil "~{~2,'0X~}" (coerce v 'list)))
>
> ah! this one I like a lot better than my code :-)
> I did play around with coerce but without success
> because I didn't think of using it to turn the
> vector into a list first (thought I could go to
> strings directly, duh)...
>
> thanks!
>
> --Sylvain


You could also do something like,

(defun digest2string (v)
(let ((*print-base* 16))
(with-output-to-string (s)
(map nil #'(lambda (e) (princ e s)) v)
s)))

which shows off some other features, and avoids format line noise, not
that I think that is so bad.

This exercise gives me a surprisingly bad feeling.

1) Format doesn't iterate over general sequences.

2) I originally expected with-output-to-string to capture princ's
output, and tried just

(with-output-to-string (s) (map nil #'princ v) s)

but of course princ still goes to *standard-ouput* without the extra
argument, which requires an anonymous function. I had expected to find
a built-in macro to divert everything destined for a stream (defaulting
to *standard-output*) to a string, then return that string.

.