Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- The (if (null? result) ...) check can be done once, before the loop. It doesn't need to be done separately on each iteration. - Carrying the result as a string, and using string-append on each iteration, is probably more efficient than gathering the strings into a temporary list. Avoiding apply will also avoid implementation limits on how many args can be passed to a procedure (if list is very long, it can exceed the limit). - Use the names `lst` and `loop` as decided in #16.
- Loading branch information
7dc5b2e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For small lists, it might be the case the "accumulate in strings" version is faster - but it potentially turns an O(n) operations into an O(n^2).
(Consider the special case where the lists consists of strings of length 1).
7dc5b2e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each iteration of the loop will allocate two pairs, which is 2 * 2 words of RAM (32 bytes). At the end we'll reverse them, which makes a copy of the list. Seems unlikely the
string-append
-only version would allocate significantly more RAM in any scenario, but you may be right :)7dc5b2e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, SRFI 13 has
string-concatenate-reverse
for this kind of thing. Though an efficient implementation of that procedure will use string ports or some moral equivalent, which will essentially duplicate what ourstring-join
is doing.7dc5b2e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW this was type of loop was discussed here two weeks ago.
https://groups.google.com/g/racket-users/c/ovQ3G_F7BDw/m/JrHn0ftzAgAJ