Reputation:
Basically, what I want is a function of the following interface:
fun stringlst2string (list0(string)): string
where stringlst2string takes a list of string and returns the concatenation of them.
Upvotes: 1
Views: 245
Reputation: 935
One can readily do this kind of thing (that is, turning one form of sequence into another form of sequence) by going through linear streams. For instance, the following code turns a list of strings into a stream of strings and then into a stream of streams (of chars) and then into a stream of chars and then into a string:
fun
stringlst2string
(
xs: list0(string)
) : string =
strptr2string
(
string_make_stream_vt
(
stream_vt_concat
((streamize_list0_elt(xs)).map(TYPE{stream_vt(charNZ)})(lam x => streamize_string_char(x)))
)
)
This is a very lean implementation in terms of memory usage, and there is no memory that is not released at the end (except for the memory needed to store the returned string). Clearly, the same approach applies if you want to concatenate an array of strings.
Upvotes: 1
Reputation: 4006
One can use the standard library function stringlst_concat
to accomplish this. See the reference for explanation and this snippet for a working example.
Upvotes: 1