1

I'm currently writing a LISP parser that iterates through some AutoLISP code and does its best to make it a little easier to read (changing prefix notation to infix notation, changing setq assignments to "=" assignments, etc.) for those that aren't used to LISP code/only learned object oriented programming.

While writing commands that LISP uses to add to a "library" of LISP commands, I came across the LISP command "progn". The only problem is that it looks like progn is simply executing code in a specific order and sometimes (not usually) assigning the last value to a variable.

Am I incorrect in assuming that for translating progn to object-oriented understanding that I can simply forgo the progn function and print the statements that it contains? If not, what would be a good equivalent for progn in an object-oriented language?

4

Everything in Lisp is an expression, meaning that it returns a value. The progn function executes a series of functions/forms, ignoring the return values of all but the last call/form, which it returns as its own return value.

E.g. in the following code

(progn
  (some-method 'a 'b 'c)
  (another-method 1 2 3)
  (final-method "x" "y" "z"))

the value returned by the call to final-method would be the value returned by the enclosing progn call.

An OOP equivalent would be:

public FinalReturnType prognImpl123()
{
    someMethod(Sym("a"), Sym("b"), Sym("c"));
    anotherMethod(1, 2, 3);
    return finalMethod("x", "y", "z");
}

As progns you encounter will likely be nested in other expressions, you'll want your translations to return something.

  • What would be the purpose of such a thing? To apply side-effects from the first two functions, but throw away their results? Why would there be side-effects in a functional language? – Robert Harvey Jun 5 '14 at 20:16
  • 2
    @RobertHarvey Yes, side-effects is the main use I can see. I usually use progn (do in Clojure) to do side-effects like printing to the command line (which I hack in while debugging). You can have side-effects in Lisp since it is not a purely functional language -- some argue it's not functional at all, just conducive to a functional style; I like to consider it functional since that's the style I use when I program in it. – paul Jun 5 '14 at 20:29
  • 2
    To my understanding, functional programming does not try to eliminating side-effects, but to separate them clearly from side-effect free code and use them only when necessary. Even a purely functional language like Haskell (every expression is referentially transparent) allows side-effects (e.g. by executing I/O actions). – Giorgio Jun 5 '14 at 20:47
  • @Giorgio Well said; when writing my comment about being able to have side-effects I was thinking mostly variable mutation or IO deep in the bowels of the code. – paul Jun 5 '14 at 21:01

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.