[Haskell-cafe] Reversing a string of words: C# v Perl V Ruby v
Haskell
Steve Downey
sdowney at gmail.com
Mon Dec 11 18:48:21 EST 2006
the typical good solution to this problem in c or c++ is to use a
string reverse function on the entire buffer, then re-reverse each
word. this leaves multiple spaces correctly embedded in the larger
string.
that approach, of course, won't work in haskell, since it relies on
updates. but if the challenge includes transforming "one two three
four " into " four three two one", how could this be done?
On 12/10/06, Neil Mitchell <ndmitchell at gmail.com> wrote:
> Hi
>
> > However, every response to this question I've seen (admittedly only three
> > or four so far) used StringBuilder. When I ask, "why not use Join?" most
> > C# programmers seem to respond that StringBuilder is "faster" (I haven't
> > measured the difference in performance).
>
> I don't know, but I'd be really surprised if Join wasn't implemented
> in terms of StringBuilder at worst. Possibly it does a summation of
> the lengths of the String's in the list, and does exactly one
> allocation, making it much more efficient. Either way, if there is a
> function in the libraries, you shouldn't be able to write one which is
> faster!
>
> I suspect people saying "use StringBuilder" have read an article on a
> website telling them that String concat is slow - but haven't thought
> about the implications...
>
> Thanks
>
> Neil
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list