Interesting framing by Carl Masak:
Code generation and stone soup
Every type of control flow in programming languages is just convenient sugar for
if
statements andwhile
loops.
if
s andwhile
s are the stone soup to which all the rest of our control flow can be added as seasoning.if
s let you conditionally skip ahead in code, andwhile
s allow you to conditionally skip back. That's all you need.
Hadn't quite heard it put it like that before.
I'm as likely as not to try and turn a given task into an operation on lists -- filter with grep
, process with map
, repeat and fade.
I guess if you could squint you could look at map
as a flavor of while
and grep
as your if
.
Us coders who still work in assembly language (and occasionally discrete logic) will be happy to point out that "while" is just an "if" and a "goto".
Of course since Perl lacks a native switch (yes, I know about "use Switch", I think he's wrong about switch being syntactic sugar for "if/else", because I've implemented switch in Perl using a hash and code references. Which, to my mind, is closer to how to think of "switch" than "if/elsif/else".