Reply to post: Re: Code Review

This is why copy'n'paste should be banned from developers' IDEs

Michael Wojcik Silver badge

Re: Code Review

You couldn't write like that in a functional language, such as Lisp or F#.

That's not correct. The original example (the copy-pasta) works just fine with a purely functional language - it needn't mutate any objects, as written. It can create a new string object for each concatenation, and indeed that's what will happen with any language that has immutable strings, including anything in the LISP or ML families (like F#), or any JVM- or CLR-based language.

It's performing destructive assignment, but that just changes what value the variable is bound to, not the object itself. In a pure functional language, you wouldn't be able to do that - you'd have to rewrite the code slightly to return the new string each time rather than assigning it to the same variable (or assign it to a new variable each time, which would be uglier but work just the same).

Neither most LISP variants nor F# (or most other ML variants) are purely functional. LISP has the "set" family of special forms, for example.

The actual series-of-if-statements style is procedural, not functional; but converting it to functional form is trivial and could easily be automated in a functional language as syntactic sugar.

The hip way to do it these days would be with a list comprehension - think a string join operation with the literal " AND " as the delimiter, filtered with "length > 0" as a predicate. And list comprehensions are notionally purely functional, though the actual underlying implementation may operate on a destructive temporary.

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Biting the hand that feeds IT © 1998–2019