Tech News
← Back to articles

Emulating Goto in Scheme with Continuations

read original related products more articles

In his 1968 letter, A case against the GO TO statement (known only by that name), Dijkstra said “[t]he go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one’s program.” Unfortunately, scheme programmers aren’t given that invitation. That’s no fair! Fortunately, scheme has a procedure, call/cc , that we can use to emulate the style of control flow that GOTO provides. We can use syntactic abstraction to invite scheme programmers to make a mess of their programs in a limited context.

How GOTO works

Odds are, you know how GOTO works, but let’s briefly review. Perhaps you’ve seen a BASIC program that looks something like this:

10 PRINT "Hello, world!" 20 GOTO 10

This, as you may have guessed, outputs:

Hello, world! Hello, world! Hello, world! Hello, world! ...

…forever.

Normally, control proceeds from the lowest line number to the highest line number, but the GOTO statement “jumps” to the given line, no matter where it is. (Forgive my imprecision, this is a basic tutorial, not a BASIC tutorial.)

You’re more likely to see goto in C :

void do_something ( ) { char * important_stuff = ( char * ) malloc ( ) ; FILE * important_file = fopen ( ) ; if ( errno != 0 ) goto cleanup ; if ( errno != 0 ) goto cleanup ; printf ( "Success!

... continue reading