For instance, the user would define a for each loop in their code as having a structure something like: foreach([variable1] : [variable2]){ [statements] } (when [ ] are placeholders) and then the pieces would be used like this: for(i = 0; i < variable2.length; i++){ variable1 = variable2[i]; statements } and then in the rest of the program, the user could use a for each loop with that format: total = 0; foreach(num : numbers){ total += num; }

I hope that example makes sense. To be clear, I am saying that in this hypothetical language a for each loop does not exist, but the user can define one which will, for then on, work exactly the same as any other structure. Are there any languages which allow this?

  • $\begingroup$ I do have a programming language that does it, but it is a little of an esoteric programming language $\endgroup$
    – MilkyWay90
    Jan 4 '19 at 18:52
  • 2
    $\begingroup$ Lisp supports that. $\endgroup$ Jan 4 '19 at 20:03

There are two aspects to your question:

  1. Can we arrange a convenient syntax to express one control structure in terms of another?
  2. Can we actually define new control flow mechanisms.

For example, suppose we have a language which only has the if statements without else:

if A then B

Can we express if A then B else C somehow? Yes, like this:

a = A ;
if a then B ;
if not a then C

It would be even better if we could package up the above piece of code into convenient syntax so that we could write just if A then B else C.

Sometimes we can define new control structures using existing ones. For instance, a while loop

while A do B

can be replaced with a call to a recursive function (under some technical assumptions):

function whileLoop():
   if A then (B ; whileLoop ())


This sort of transformation is a bit more involved because it requires that for each while loop we define a new recursive function. If we were to automate the process, we would have to worry about many details (how to invent unique names of generated functions, do the functions have access to the local variables, etc.) than when we're performing just a straightforward syntactic translation of one variant of loop to another.

On the other hand, there are control mechanism which simply are not expressible in a language, unless already supported. One such example would be continuations. You can't get these in C, for example, but they exist in Scheme.

Your question is about syntax: can we make a syntactic transformation that replaces one piece of syntax with another piece of syntax. Languages usually deal with this sort of thing through a macro mechanism or syntax extensions. Here are some examples:

  • C has macros which will get you some of the way
  • Scheme has a more elaborate macro system which gets you a lot further
  • Racket, a descendant of Scheme, has an elaborate multi-stage computation system which allows essentially arbitrary syntax extensions
  • OCaml has the camplp5 syntax extension tool which allows pretty much arbitrary syntax extensions
  • Template Haskell is an extension of Haskell which allows meta-level transformations of syntax.

I am sure there are many other languages with similar facilities that I am not familiar with.

  • $\begingroup$ Converge was specifically designed as a language with an expressive macro system despite having complex Python-like syntax. $\endgroup$ Jan 6 '19 at 12:56
  • $\begingroup$ @JörgWMittag: a link to the language would be nice. $\endgroup$ Jan 6 '19 at 16:21
  • $\begingroup$ Here you go: convergepl.org $\endgroup$ Jan 6 '19 at 17:49

In FORTH control structures are defined in code. I think Smalltalk had some related features.

  • $\begingroup$ Indeed. Conditionals / Booleans in Smalltalk are essentially isomorphic to the Church Encoding of Booleans in λ-calculus. Loops are methods on Closures that take Closures as arguments. Classic for loops are methods on Integers that take Closures as arguments. Collections know how to iterate themselves, map themselves, fold themselves, etc. Really, the only control structure built into the language is runtime-polymorphic dynamic message dispatch, aka virtual method calls. The fact that the Stack is just another Smalltalk object that can manipulated allows implementing arbitrary control. $\endgroup$ Jan 6 '19 at 13:01

My programming language, MICGBF, solves your problem.

Any time you make a program, it turns that program into a single command, using a self-changing interpreter.

For example, if you give it the commands ������ and run it, then my programming language would make a new command called <insert tab here> which would execute ������. This could be helpful for making long programs short.

The proof for this is in the Python 3 code


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.