Feb
22

Clojure style hash table syntax for common lisp.

;;; helper function
(defun make-pairs (lst)
(cond ((null lst) lst)
(t (cons (list (first lst) (second lst)) (make-pairs (cddr lst))))))

;; convert a list into a hash
(defun list-to-hash (lst)
(let ((hash (make-hash-table :test #'equal)))
(dolist (x (make-pairs lst) hash)
(destructuring-bind (key value) x
(setf (gethash key hash) value)))))

(defun read-hash (stream char)
"function to be called when the reader sees '{'"
(list-to-hash (read-delimited-list #} stream t)))

;; macro making code
(set-macro-character #{ #'read-hash)

(set-macro-character #} (get-macro-character #)))

Does not support the options that make hash table supports and also, as you can see, my implementation uses ‘equal’ for testing the key instead of using ‘eql’.

CL-USER> { :a 1 :b 2 :c 3 }
#<HASH-TABLE :TEST EQUAL :COUNT 3 {B13BB81}>
CL-USER>

The point of this exercise is not to introduce extra syntax into common lisp, but to show how easy it is to do it in lisp.

Jul
07

Good and freely available Common Lisp books that I have come across.

Gentle Introduction to Symbolic Computation a.k.a GISC

This is a slow moving book, but once you are through, your programming fundamentals will be strong.

Practical Common Lisp by Peter Seibel

True to its name, this book shows how Common lisp can be used to write practical applications.

OnLisp by Paul Graham

This book is all about functional programming using common lisp and lots of macros. Speaking from personal experience, this book has the ability to demoralize you and make you feel like a complete idiot if you are impatient.

Jun
22

Code below illustrates how to render JSON and raw HTML/TEXT from a controller.



-module(mock_controller).
-export([index/1, mochi_test/1, lshift_test/1, yaws_test/1]).

index(A) ->
 { response, [ { html, <<"<b>hello json</b>">> } ] }. %% renders raw html code

%% encoding a tuple

mochi_test(A) ->
 { response, [ { html, mochijson2:encode({struct, [{'mochi', 'json2'}]}) } ] }. %% mochiweb JSON implementation

lshift_test(A) ->
 { response, [ { html, rfc4627:encode({obj, [{'lshift','rfc4627'}]})} ] }. %% Lshift implementation of JSON

yaws_test(A) ->
 { response, [ { html, json:encode({struct, [{"yaws", "json"}]}) } ] }. %% YAWS json 

You need to download the JSON implementation and place it in the ’src’ directory of your Erlyweb application
directory. Lshift and Mochi JSON modules.

Jun
09

I had a hard time getting postgres to work with Erlyweb but finally got it working with help from people on the erlyweb mailing list and IRC. So for all the Erlyweb noobs out there, here’s a step by step configuration guide for configuring Erlyweb with postgres as the database.

1. pg_hba.conf:

Configure your database to use MD5 for authentication. The Erlyweb postgres driver connects to the database using a MD5 connection. So your pg_hba.conf should contain the following line:

# TYPE DATABASE USER CIDR-ADDRESS METHOD
host <db name> <user> 127.0.0.1/32 md5

My pg_hba.conf looks like this:

# TYPE DATABASE USER CIDR-ADDRESS METHOD

host erlytest abhi 127.0.0.1/32 md5

The above line instructs postgres to allow local connections to the database named ‘erlytest’ for the postgres user ‘abhi’ using a md5 encrypted connection.

Note that the order that the rule appears is important. Refer postgres documentation for more information.

Restart the postgres service for the new changes to take effect.

2. psql.app

Create a file named psql.app in the ‘ebin’ directory of your Erlyweb application. The file should look something like this:

{application, psql, [{description, "application description"},
{vsn, "0.0.2"},
{modules, []},
{registered, [psql_sup]},
{applications, [kernel, stdlib]},
{mod, {psql, []}},
{env, [{erlydb_psql, {"localhost", 5432, "<postgres username>", "<password>", "<dbname>"}},
{pools, [{erlydb_psql, 1}]}]}]}.

3. Code Path

Add ‘ebin’ to your erlang code path in erl using:

code:add_path("/path/to/erlywebapp/ebin/").

4. Start yaws in interactive mode:

sudo yaws -i

5. Connect to postgres database:

application:start(psql).

6. Compile your application in yaws REPL:

erlyweb:compile("/path/to/erlyweb/app/", [{erlydb_driver, psql}]).

… and thats about it. Happy hacking!