5 ms·
Actually, C's string handling is very well-suited to this problem. (Which doesn't mean it is perfect - in fact, it's highly inconvenient for many tasks. Just no
by JoachimSchipper 16y ago
Actually, C's string handling is very well-suited to this problem. (Which doesn't mean it is perfect - in fact, it's highly inconvenient for many tasks. Just not this one.)
- eru 16y agoYes, it is. But C's other nuts and bolts are so low level that they introduce lots of variability. In e.g. Haskell everyone would just do condense_by_removing :: Char -> String -> String condense_by_removing c = filter (/=c) Because there's much less going on here--the loop being more or less implicit only--there's less room for variations. Mind you, Haskell programs also show lots of variability if the tasks gets only slightly more complicated.
- dkersten 16y agoThe Haskell snippet doesn't remove the character in place though, does it? I imagine solutions would be more varied then.
- lincolnq 16y agoNo. "In place" has virtually no meaning in Haskell. I find this very interesting -- a popular idiom in one language can't even be represented in another.
- sesqu 16y agoOne of the biggest reservations I initially had about abstraction, when learning programming, was that I didn't consider performance a detail. As such, I didn't litter my code with objects or pure functions even when they made conceptual sense, unless I was convinced the overhead was small and the abstraction helpful. I've found that languages like Java make assumptions about your memory, and languages like Prolog make assumptions about your data. I'm under the impression that in Haskell in-place modification is an interpreter optimization, whereas C is increasingly employed when resources are constrained and abstraction layers are deemed too costly. Consequently, C is maligned for resulting in faulty implementations behind every corner, yet it remains one of the most used languages.
- eru 16y agoThe Haskell compiler is free to use in-place mutations, when it can prove that nobody can access the old object any longer. The language Clean supports this notion explicitly in its type system (and uses it for IO, instead of, say, Monads like Haskell).
- eru 16y agoYou can do replace stuff in-place (and other low-level stuff) in the IO Monad.