3 ms·
Strings are usually represented as arrays (Haskell represents them as linked lists; it's the only exception I can think of offhand), which are a fixed length. T
by kbp 9y ago
Strings are usually represented as arrays (Haskell represents them as linked lists; it's the only exception I can think of offhand), which are a fixed length. To append to an array, you need to allocate a larger one and copy everything over. You can avoid that copying for a lot of appends by allocating more space than you need and using a fill pointer, and the realloc might be hidden away in a method, but it is still fundamentally a fixed-length data structure.
Linked lists are not a fixed lenth. To add an element to a linked list, you allocate a new cons, point its car to the new element, and point its cdr to the old list. There is no "and then allocate a new, larger fixed-length list, copy everything over, and delete the old one" step.
That's all language-agnostic, but in Scheme specifically, the interface it provides for strings does not simulate arbitrary length, either. The only way to add onto strings is using string-append, which returns a newly allocated string.