Search code examples
emacslispslime

Learning Common Lisp tips for a Windows/C++ programmer


I'm an experienced C++/.NET/Java Windows/web programmer trying to learn (Common) Lisp. I'm reading Practical Common Lisp and using SLIME.

I'm getting the Lisp language easily enough, but I'm having trouble groking the mechanics of development. One of my issues is dealing with Emacs. I have no experience with it and find it generally confusing with hard to find/figure out commands.

Specific questions:

  • I get the REPL, but I don't quite get how I can use it effectively. When I need to change a function I have to retype the defun and make changes (tedious and error prone). How can I do this better?
  • How do I get from entering code at the REPL to actually having a program? I'm used to the C model where you have code files that you can review, edit and recompile. I know Lisp has something similar (via the load function), but how does one get a function I just defined and tested into a file to be saved? I'm hoping there's a better method than my current select+copy+paste.
  • How do you debug errors? Is there any ability to step into code like I would with other languages?
  • As long as the S-expression is valid, I don't get any errors when entering a (defun ...). It's not until I try to use it that I find out it's not valid. Is there any way to get errors sooner (i.e. compile the code)?
  • Is there some IDE that would be more familiar to me or allow me to play with the language easier?
  • Should I switch to learning Scheme instead?

Any tips would be greatly appreciated!


Solution

  • -I get the REPL, but don't quite get how I can use it effectively. When I need to change a function I have to retype the defun and make changes (tedious and error prone). How can I do this better?

    -How do I get from entering code at the REPL to actually having a program? I'm used to the C model where you have code files that you can review, edit and recompile. I know lisp has something similar (via the load function), but how does one get a function I just defined and tested into a file to be saved? I'm hoping there's a better method than my current select+copy+paste.

    Load SLIME. Enter code in your .lisp file, and then run slime-eval-buffer to load all your code into Lisp. Then, for a specific function you are hacking on C-e, C-j to redefine it in your running Lisp.

    -How do you debug errors? Is there any ability to step into code like I would with other languages?

    Yes. (trace 'my-function) - or some variant on that - and when my-function is called, it will break into the debugger.

    -As long as the S-expression is valid, I don't get any errors when entering a (defun ...). It's not until I try to use it that I find out it's not valid. Is there any way to get errors sooner (i.e. compile the code)?

    To some degree, that is a characteristic of dynamic languages (Python, Perl, Ruby, Lisp, etc.). Try SBCL for better error-checking.

    -Is there some IDE that would be more familiar to me or allow me to play with the language easier?

    Emacs is the free IDE for Lisp. Allegro has a free edition I believe; I've never tried it though..

    -Should I switch to learning Scheme instead?

    Nah, Scheme is not as pragmatic a language.