Difference between revisions of "Common Lisp"

From RogueBasin
Jump to navigation Jump to search
 
(7 intermediate revisions by 5 users not shown)
Line 8: Line 8:
*[http://dto.github.io/notebook/sanctuary.html Sanctuary]
*[http://dto.github.io/notebook/sanctuary.html Sanctuary]
*[[Invader Tactics]]
*[[Invader Tactics]]
*[[Laser Spigot]]
*[[The Rougelike]]
*[[The Rougelike]]
*[[The Sewer Massacre]]
*[[The Sewer Massacre]]
Line 13: Line 14:
*[http://sourceforge.net/projects/motm/ Menace of the Mines]
*[http://sourceforge.net/projects/motm/ Menace of the Mines]
*[https://github.com/nowl/detome detome]
*[https://github.com/nowl/detome detome]
*[[Lingwall]]
*[[City of the Damned]]
*[https://awkravchuk.itch.io/lispy-rogue Lispy Rogue]


==The advantages==
==The advantages==
Line 20: Line 24:
*Common Lisp is a high-level language. There is built-in support for various data structures: linked lists, arrays, hash tables, and so on, and the standard library contains a lot of useful functions to operate on them. Common Lisp is dynamically typed and garbage collected.
*Common Lisp is a high-level language. There is built-in support for various data structures: linked lists, arrays, hash tables, and so on, and the standard library contains a lot of useful functions to operate on them. Common Lisp is dynamically typed and garbage collected.
*Unlike the recent trend of "scripting languages", Common Lisp code is compiled, and may achieve performance comparable to C and friends.
*Unlike the recent trend of "scripting languages", Common Lisp code is compiled, and may achieve performance comparable to C and friends.
*Common Lisp is partly functional. That means it allows many features not found in other languages, such as first-class functions. Unfortunately, that makes it harder to learn than most languages.
*Common Lisp is multi-paradigm: its procedural, object-oriented, functional, meta, reflective and generic. That means it allows many features not found in other languages, such as first-class functions. Unfortunately, that makes it harder to learn than most languages. It has the most powerful object system known to mankind, CLOS, which supports multimethods, multiple inheritance, method combinations and is fully dynamic to boot.
*Common Lisp is partly object oriented. It has the most powerful object system known to mankind, CLOS, which supports multimethods, multiple inheritance, method combinations and is fully dynamic to boot.
*Lisp programs are usually developed interactively. Instead of the usual edit-compile-run cycle, Common Lisp developer incrementally compiles, recompiles and tests individual functions to save his time. Lisp's famous REPL prompt allows to execute any Lisp code in the middle of writing your program. Using such tools as Emacs-based SLIME allows for even more progressive techniques.
*Lisp programs are usually developed interactively. Instead of the usual edit-compile-run cycle, Common Lisp developer incrementally compiles, recompiles and tests individual functions to save his time. Lisp's famous REPL prompt allows to execute any Lisp code in the middle of writing your program. Using such tools as Emacs-based SLIME allows for even more progressive techniques.
*The problem of repetitive code and "patterns" is solved by Lisp macros, which are much, much more powerful than their C counterparts.
*The problem of repetitive code and "patterns" is solved by Lisp macros, which are much, much more powerful than their C counterparts.
Line 31: Line 34:


==Learning the language==
==Learning the language==
[http://www.gigamonkeys.com/book/ Practical Common Lisp] is usually considered the best introductory book about CL. What's good about this book is how it really opens up the whole lot of possibilities, without the fawning advocacy that you'll find in some books on Ruby and similar trendy languages.  
[http://www.gigamonkeys.com/book/ Practical Common Lisp] is usually considered the best introductory book about CL. What's good about this book is how it really opens up the whole lot of possibilities, without the fawning advocacy that you'll find in some books on Ruby and similar trendy languages. Get a copy of [http://common-lisp.net/project/slime/ SLIME] (Superior Lisp Interaction Mode for Emacs), its a lisp-mode for the Emacs text-editor which'll make things easier. It may take some time to get comfortable to Emacs but it is surely a powerful tool for Common-Lisp developers.


Also check comp.lang.lisp Usenet newsgroup, which is frequented by the most skilled users of the language.
Also check comp.lang.lisp Usenet newsgroup, which is frequented by the most skilled users of the language.
Line 37: Line 40:
==Can I use [[Curses]] on Lisp?==
==Can I use [[Curses]] on Lisp?==


Yes, and you can use other C libraries (such as SDL and OpenGL) too. You can even use them more effectively, since you'll still have the advantages of interactive development (as with any Lisp program). This is done using Foreign Function Interface, such as [http://common-lisp.net/project/cffi CFFI]. You'll have to write a library wrapper that maps the functions of C library to Lisp functions, or use someone else's work. The wrappers for SDL and OpenGL are easily found through Google. The wrapper for PDCurses (and other curses variations) was written by [[Timofei Shatrov]] (get curses.lisp from the sources of his latest game). However it takes some work to set it up.
Yes, and you can use other C libraries (such as SDL and OpenGL) too. You can even use them more effectively, since you'll still have the advantages of interactive development (as with any Lisp program). This is done using Foreign Function Interface, such as [http://common-lisp.net/project/cffi CFFI].  
 
You'll have to write a library wrapper that maps the functions of C library to Lisp functions, or use someone else's work. The wrappers for SDL and OpenGL are easily found through Google. The wrapper for PDCurses (and other curses variations) was written by [[Timofei Shatrov]] (get curses.lisp from the sources of his latest game). However it takes some work to set it up.


By now, I'll assume you're using [http://common-lisp.net/project/slime/ SLIME] with CLISP. The basic setup ''won't'' work here, because curses and Emacs don't play well together. Here's a workaround: start CLISP separately and type:
By now, I'll assume you're using [http://common-lisp.net/project/slime/ SLIME] with CLISP. The basic setup ''won't'' work here, because curses and Emacs don't play well together. Here's a workaround: start CLISP separately and type:
Line 57: Line 62:


Look how the text appears in the other window. You can start from here and incrementally develop the entire roguelike!
Look how the text appears in the other window. You can start from here and incrementally develop the entire roguelike!
===State of Lisp ncurses support in 2020===
As of April 2020, known Lisp bindings for ncurses are [https://www.cliki.net/croatoan croatoan], [https://www.cliki.net/cl-charms cl-charms] and [https://www.cliki.net/cl-ncurses cl-ncurses]. croatoan and cl-charms are based on CFFI and actively developed, cl-ncurses was based on the less portable UFFI and has not been touched for some ten years now.
All three of them are available in the popular Common Lisp library manager [https://www.quicklisp.org/beta/ Quicklisp].
By using Quicklisp, the libraries can be installed as easily as calling
(ql:quickload "croatoan")
Then, provided that ncurses is installed, the library can be used as easily as:
(in-package :croatoan)
(defun test1 ()
  (with-screen (scr :input-echoing nil :input-blocking t :enable-colors t)
    (clear scr)
    (move scr 2 0)
    (format scr "Type chars. Type q to quit.~%~%")
    (refresh scr)
    (setf (color-pair scr) '(:yellow :red)
          (attributes scr) '(:bold))
    (event-case (scr event)
      (#\q (return-from event-case))
      (otherwise (princ event scr)
                  (refresh scr)))))
Some examples can be seen here:
* [https://imgur.com/iywc1iG Menus and extended colors]
* [https://imgur.com/COlPTYG Dialogs and forms]
* [https://www.youtube.com/watch?v=H-vb0tqkP-E Video of the evolution game] from the game book [http://landoflisp.com/ Land Of Lisp].
[[User:McParen|McParen]] ([[User talk:McParen|talk]]) 13:15, 23 June 2018 (CEST)


[[Category:Programming languages]]
[[Category:Programming languages]]

Latest revision as of 14:36, 5 November 2024

Common Lisp is a multi-paradigm programming language, which was used for programming roguelikes several times.

Known Common Lisp roguelikes

The advantages

The ANSI standard of Common Lisp was written about 20 years ago. Let's look at the advantages that Common Lisp had and still has over most other programming languages.

  • Common Lisp is a high-level language. There is built-in support for various data structures: linked lists, arrays, hash tables, and so on, and the standard library contains a lot of useful functions to operate on them. Common Lisp is dynamically typed and garbage collected.
  • Unlike the recent trend of "scripting languages", Common Lisp code is compiled, and may achieve performance comparable to C and friends.
  • Common Lisp is multi-paradigm: its procedural, object-oriented, functional, meta, reflective and generic. That means it allows many features not found in other languages, such as first-class functions. Unfortunately, that makes it harder to learn than most languages. It has the most powerful object system known to mankind, CLOS, which supports multimethods, multiple inheritance, method combinations and is fully dynamic to boot.
  • Lisp programs are usually developed interactively. Instead of the usual edit-compile-run cycle, Common Lisp developer incrementally compiles, recompiles and tests individual functions to save his time. Lisp's famous REPL prompt allows to execute any Lisp code in the middle of writing your program. Using such tools as Emacs-based SLIME allows for even more progressive techniques.
  • The problem of repetitive code and "patterns" is solved by Lisp macros, which are much, much more powerful than their C counterparts.

As you can see, Common Lisp was way ahead of it's time in 80s, and it's still ahead now. Maybe you could use this alien technology to get ahead of other roguelike developers?

Choosing an implementation

There are a lot of Common Lisp implementations, and they are mostly quite good. SBCL is an industrial-strength free implementation, and generally considered the best free Lisp for stuff like web-applications or where speed does matter. However, as a roguelike developer you'll want to choose CLISP. It is somewhat slower than SBCL, but two things set it apart: it runs quite well on Windows, and the executables it produces are not very big (<10 mb, compressed to ~3mb), unlike mammoth-sized SBCL images. Anyway, if you'll stick to ANSI standard and portable libraries, there's little difference between various Lisps.

Learning the language

Practical Common Lisp is usually considered the best introductory book about CL. What's good about this book is how it really opens up the whole lot of possibilities, without the fawning advocacy that you'll find in some books on Ruby and similar trendy languages. Get a copy of SLIME (Superior Lisp Interaction Mode for Emacs), its a lisp-mode for the Emacs text-editor which'll make things easier. It may take some time to get comfortable to Emacs but it is surely a powerful tool for Common-Lisp developers.

Also check comp.lang.lisp Usenet newsgroup, which is frequented by the most skilled users of the language.

Can I use Curses on Lisp?

Yes, and you can use other C libraries (such as SDL and OpenGL) too. You can even use them more effectively, since you'll still have the advantages of interactive development (as with any Lisp program). This is done using Foreign Function Interface, such as CFFI.

You'll have to write a library wrapper that maps the functions of C library to Lisp functions, or use someone else's work. The wrappers for SDL and OpenGL are easily found through Google. The wrapper for PDCurses (and other curses variations) was written by Timofei Shatrov (get curses.lisp from the sources of his latest game). However it takes some work to set it up.

By now, I'll assume you're using SLIME with CLISP. The basic setup won't work here, because curses and Emacs don't play well together. Here's a workaround: start CLISP separately and type:

(load "/path/to/asdf.lisp")
(load "/path/to/slime/swank.asd")
(asdf:oos 'asdf:load-op :swank)
(ext:saveinitmem "clispswank" :quiet t :norc t :executable t
                  :init-function #'swank:create-server)

Now, you'll have executable file called clispswank somewhere. Run it, and in Emacs type M-x slime-connect and press Enter until it connects. Now, you're in a good shape to start using curses. Load/compile CFFI, trivial-gray-streams and curses.lisp (you can automate this process by writing an appropriate asd file). Then you can start playing with it:

(in-package :curses)
(connect-console)
(erase)
(attrset :cpurple)
(mvaddstr 2 2 "Hello World!")
(refresh)

Look how the text appears in the other window. You can start from here and incrementally develop the entire roguelike!

State of Lisp ncurses support in 2020

As of April 2020, known Lisp bindings for ncurses are croatoan, cl-charms and cl-ncurses. croatoan and cl-charms are based on CFFI and actively developed, cl-ncurses was based on the less portable UFFI and has not been touched for some ten years now.

All three of them are available in the popular Common Lisp library manager Quicklisp.

By using Quicklisp, the libraries can be installed as easily as calling

(ql:quickload "croatoan")

Then, provided that ncurses is installed, the library can be used as easily as:

(in-package :croatoan)

(defun test1 ()
  (with-screen (scr :input-echoing nil :input-blocking t :enable-colors t)
    (clear scr)
    (move scr 2 0)
    (format scr "Type chars. Type q to quit.~%~%")
    (refresh scr)
    (setf (color-pair scr) '(:yellow :red)
          (attributes scr) '(:bold))
    (event-case (scr event)
      (#\q (return-from event-case))
      (otherwise (princ event scr)
                 (refresh scr)))))

Some examples can be seen here:


McParen (talk) 13:15, 23 June 2018 (CEST)