Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to get a Lisp "hello world" web application going

I want to write a Lisp web application just for fun. I found this question about Lisp web apps but all the answers seem so complicated. After looking into the links provided in the answers, the solutions seem really complicated.

If I just want a simple, "hello world" Lisp web app, is there not a simple way to do it?

like image 784
Jason Swett Avatar asked Dec 29 '10 21:12

Jason Swett


People also ask

How do you say hello world in lisp?

Unfortunately, Lisp has many flavors which means the following implementation of Hello World will likely only be applicable to handful of those flavors: (format t "Hello, World!") (format t "Hello, World!")

How do I run a lisp program?

Step 1: After logging into a CUIT machine, enter "lisp" after the $ shell prompt and then hit <return>. Another way is to run lisp via emacs: Meta-x run-lisp (i.e. hit 'esc' followed by 'x', type "run-lisp" and you'll be in lisp mode from which you can load files of lisp code...)

Is lisp good for web development?

It's relatively fast, it's easy, and everyone supports it. However, if you are building a more complicated application, don't mind doing VPS hosting (Linode, AWS, etc), and want all the wonderful features I listed above, go with lisp.


2 Answers

This answer LOOKS complicated, but I think that getting a simple Lisp web app up and running is going to be easier than learning the other more awesome bits of Lisp anyway, so it's probably worth it.

There's a couple of really great Common Lisp books with intro-to-web-app chapters: Practical Common Lisp and Land of Lisp.

There's a chapter in Land of Lisp which covers building a simple web server using sockets. It's pretty rudimentary, but I think would serve as a great starting point for a "hello world" type of Lisp app.

The chapter in Practical Common Lisp is at a higher level, and works with a full-fledged server called Allegro Serve. There are later chapters which build an MP3 streaming app.

Practical Common Lisp is available for free, here's the chapter of interest: http://gigamonkeys.com/book/practical-web-programming-with-allegroserve.html

I think both books are great resources for starting out with Common Lisp (as someone who's just starting out myself), although Land of Lisp is a bit more accessibile and more fun, although it does cover some interesting problems like lazy evaluation and searching game trees. Practical Common Lisp is more... practical, but that's not really a bad thing. It's aimed at professional programmers so its tone is just a little more serious.

One word of warning:

AFAIK Common Lisp doesn't have a really standard way of doing network programming, so this is one area of Lisp learning where you start to run into problems if you don't pick the same implementation as the book you happen to be reading.

Land of Lisp uses CLisp throughout, but you can use SBCL if you follow along with this blog post nearby: http://blog.ciaranbradley.com/crossing-the-streams-land-of-lisp-chapter-12-0

Practical Common Lisp uses Allegro Serve as I said, and I think there is a version of Allegro Lisp available from their site for use with the book. However, you can also use Portable Allegro Serve. Be careful if you are using Mac OS X and SBCL (as I am): SBCL's thread support is experimental on OS X, so if you go that route, the best bet is to install Ubuntu in a VM and then apt-get install sbcl and do your Allegro Serve programming in there, where SBCL threads are better supported. Maybe some CL wizards can suggest some other tips here. That's just what worked for me.

like image 180
michiakig Avatar answered Oct 02 '22 19:10

michiakig


For CL-HTTP one would load the server into Lisp and do:

(defun hello-world (url stream)
  (http:with-successful-response (stream :text)
    (princ "hello world" stream)))

Above is a response function. The response function has two arguments: an URL and a stream. The response function adds the usual response headers and says that it returns 'text'. Within this we just print a string to the output stream.

(http:export-url #u"/hello-world"
                 :computed
                 :response-function 'hello-world)

Above exports an URL that is merged with the default context (the default server name and port). The #u is a read macro to create URL objects. The URL is exported as :COMPUTED and thus needs a :RESPONSE-FUNCTION to compute the response. We pass in the function we defined above.

When a client sends a GET request with the URL to this server, it calls the response function for that URL and supplies an output stream.

Which then generates this content:

CL-USER 4 > (http:show-raw-url #u"/hello-world")

Status Code: 200 (OK)
Server Version: http/1.1
Date: Wed, 29 Dec 2010 23:39:52 GMT
Server: CL-HTTP/70.218 (LispWorks; 2.1.8)
Content-Type: text/plain; charset=ISO-8859-1
Transfer-Encoding: chunked

hello world

That's it.

like image 44
Rainer Joswig Avatar answered Oct 02 '22 20:10

Rainer Joswig