Re: sanity check (coding style)
- From: Rainer Joswig <joswig@xxxxxxx>
- Date: Wed, 30 Nov 2005 05:47:49 +0100
In article <udSdncpTKYqaxRbeRVn-iQ@xxxxxxxxxxxxx>,
Sylvain <sryl@xxxxxxx> wrote:
> Replying to myself (sorry for the bad form):
> > (defun sha2string (v)
> > (let ((s nil))
> > (map 'vector
> > #'(lambda (n)
> > (setf s (concatenate 'string s
> > (format nil "~2,'0,,X" n))))
> > v)
> > s))
>
> what I had initially in mind, which looked more
> 'lispy' to me (because it doesn't use side effects)
> was the following:
>
> (defun sha2str (v &optional (i 0))
> (if (eql i (length v))
> ""
> (concatenate 'string
> (format nil "~2,'0,,X" (aref v i))
> (sha2str v (1+ i)))))
>
> well, I kind of shy away from recursion when I
> can easily avoid it, but this one doesn't use
> side effect... is there a clear cut style
> preference in such cases, or is it pretty
> much a matter of personal taste?
But why don't you shy away from a completely inefficient
implementation for a relatively low-level operation
then? This is exactly the style of programming that
Lisp makes look very slow and inefficient.
- Recursion in Common Lisp has limits. Don't use
it like this.
CL-USER 2 > (sha2str (map 'vector 'identity (loop for i below 5000 collect 13)))
Stack overflow (stack size 16000).
1 (continue) Extend stack by 50%.
2 Extend stack by 300%.
3 (abort) Return to level 0.
4 Return to top loop level 0.
Type :b for backtrace, :c <option number> to proceed, or :? for other options
CL-USER 3 : 1 > (apropos "LIMIT" "CL")
- FORMAT used to output two hex characters.
Most implementations have very inefficient FORMAT
implementations.
- CONCATENATE called in a recursive way,
will produce tons of garbage.
? (sha2str #(0 1 2 3 4 5 6 7 8 9))
Calling (CONCATENATE STRING "09" "")
CONCATENATE returned "09"
Calling (CONCATENATE STRING "08" "09")
CONCATENATE returned "0809"
Calling (CONCATENATE STRING "07" "0809")
CONCATENATE returned "070809"
Calling (CONCATENATE STRING "06" "070809")
CONCATENATE returned "06070809"
Calling (CONCATENATE STRING "05" "06070809")
CONCATENATE returned "0506070809"
Calling (CONCATENATE STRING "04" "0506070809")
CONCATENATE returned "040506070809"
Calling (CONCATENATE STRING "03" "040506070809")
CONCATENATE returned "03040506070809"
Calling (CONCATENATE STRING "02" "03040506070809")
CONCATENATE returned "0203040506070809"
Calling (CONCATENATE STRING "01" "0203040506070809")
CONCATENATE returned "010203040506070809"
Calling (CONCATENATE STRING "00" "010203040506070809")
CONCATENATE returned "00010203040506070809"
"00010203040506070809"
Since you know that your input is a vector (which has a length),
you can allocate a string with the right size and fill it with
the right contents.
>
> --Sylvain
.
- References:
- sanity check (coding style)
- From: Sylvain
- Re: sanity check (coding style)
- From: Sylvain
- sanity check (coding style)
- Prev by Date: Re: FORMAT ~<...~:@> PUZZLE
- Next by Date: Re: Hints on recursion
- Previous by thread: Re: sanity check (coding style)
- Next by thread: Re: sanity check (coding style)
- Index(es):
Relevant Pages
|