Hacker News new | ask | show | jobs
by mtreis86 1740 days ago
There are a couple ways to approach it in Common Lisp, probably a few more than I have here.

One, optional and keyword arguments can have defaults.

  (defun calc-formula (ia ib &optional (gain t))
    ...)
Two, you could use a dynamic variable and a closure. Outside the body of the let the gain var returns t, within the body of the let it returns nil.

  (defvar *gain-enabled* t)

  (defun calc-with-gain (ia ib)
    ...)

  (let ((*gain-enabled* nil))
    (defun calc-without-gain (ia ib)
      ...))
1 comments

DEFVAR declares a variable to be special. That causes ALL uses of that variable to use dynamic binding.

The function inside a LET with a dynamic variable does not create a closure. If one calls CALC-WITHOUT-GAIN later, there is no binding - unless there is another dynamic binding of that variable by a different LET active.

Thanks for correcting me. I know there is a way to do something similar to what I had in there, but I don't remember what it was. Any idea?