Re: sanity check (coding style)



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
.



Relevant Pages

  • Re: The machine stack and the C language
    ... functions in C implies a logical stack. ... I've used such implementations. ... No need for recursion there. ... You had to impose caller-saved and callee-saved registers by convention, jumping to the return register address, and since it had 32 registers you could actually go quite deep in trivial functions before needing to resort to putting out data to memory. ...
    (comp.lang.c)
  • Re: Why do lisps have stack limits?
    ... have as its implication that stack limits were bad, ... reasons for wanting deep recursion, and for this one can just write a ... pre-allocate lots of stack (for other implementations). ... context of resource limitations. ...
    (comp.lang.lisp)
  • Re: Any use for recursion?
    ... In modern languages there is no overhead. ... popped back off the stack, the program counter being changed again. ... Recursion is ubiquitous in modern software. ...
    (comp.programming)
  • Re: Memory management strategy
    ... >>trading memory for speed ... The use of registers instead of the stack doesn't need inlining. ... >longer instructions to access smaller data than they were optimized for. ... square) but it didn't need recursion or some kind of stack. ...
    (comp.lang.c)
  • The machine stack and the C language
    ... some obscure implementations somewhere in extremely small circuits ... functions in C implies a logical stack. ... <end quote> ... part of all C implementations use a hardware stack because the ...
    (comp.lang.c)