lisp

Automatically Reload a File in LISP When a Command Is Entered


I'm learning LISP for a class. I have a basic workflow setup in Ubuntu with my LISP file in VIM and an interactive LISP prompt in a terminal that I'm using to test code as I write it. Is there a way to get LISP to load a specific file every time I type a command? It's getting a bit tiring having to constantly input (load 'initial-code.cl) (yes, even when I am using the terminal's history).


Solution

  • Can always try:

    (let (fn)
      (defun l (&optional filename)
        (if filename
          (setf fn filename))
        (load fn)))
    

    Works like this:

    [2]> (l "x.lisp")
    ;; Loading file x.lisp ...
    ;; Loaded file x.lisp
    T
    [3]> (l)
    ;; Loading file x.lisp ...
    ;; Loaded file x.lisp
    T
    [4]> 
    

    Pretty simple.

    You can also do something like:

    (defun go ()
      (load "project.lisp")
      (yourfunc 'your 'parameters))
    

    Then you just type (go) and it reloads your file and calls your main entry point.

    Or even combine them:

    (defun gogo (&rest args)
        (l) ;; call (l "file.lisp") first to initialize it
        (apply #'yourfunc args))
    

    then you can change your parameters easily

    (gogo 1 2)
    (gogo 2 4)
    

    Ya know, it's lisp. Don't like something, change it.

    With more time, you can write a simple wrapper that can build these on the fly. But you get the idea.