Primarily technical blog on Lisp, .NET, C# development.

Friday, December 11, 2009

Ubuntu and FreeTDS

One of the things I have in mind is to be able to connect to a MS SQL database from Lisp in one of my projects. After spending a couple hours searching for a way to get freeTDS and odbcunix to work together, I found this blog in portuguese with a good step by step process.

I have to point out I didn't have to build freeTDS from the source. I got from the repository. I didn't use the tsql tool. Instead I used the isql one. Other than that everything is according to how I proceeded.

How to setup Freetds using Ubuntu

Friday, October 30, 2009

Installing LISP with CLBuild on Ubuntu


http://club-ubuntu.org/blogs/em/getting-started-with-lisp-in-ubuntu

then with your clbuild already installed:

./clbuild update sbcl
./clbuild compile-implementation sbcl
./clbuild install --main-projects
./clbuild install weblocks
./clbuild install cl-prevalence

Monday, October 19, 2009

clbuild and lispbuilder-sdl - update

I retested the latest version of lispbuilder-sdl, updating and recompiling from cl-build and I am glad to say it is working fine, as expected, now.

Always good to spread good news, isn't it?

Friday, August 21, 2009

clbuild and lispbuilder-sdl

On my earlier version of the alarm clock I used the cl-sdl library. I was testing clbuild the other day and I tried to use its version of the sdl: lispbuilder-sdl. Unfortunately on my machine I was getting an error everytime the app was about to close the window due to a call to SDL-glue-SDL-Close-Audio method on the cffi part of the lispbuilder-sdl library:


I suppressed this message by calling a method I knew was working and it worked fine so far.

So if you want to try the same thing, look for the glue.lisp file on the clbuild/source
/lispbuilder-sdl/cffi and change this part to the following code:

#+lispbuilder-sdl-audio

(cffi:defcfun ("SDL_CloseAudio"
SDL-glue-SDL-Close-Audio) :void)

So instead of calling the original method I am using the SDL_CloseAudio and it is working fine so far.

Tuesday, August 18, 2009

Little Alarm Clock in LISP

I wrote this code one of these days because I left the food heating a little too long. I actually burned the pan itself. Anyway it was a little fun project. You give set the time you want it to play a song from your hard drive. Using SDL I was able to play the song and also display a little window to notify about the alarm. You need cl-sdl and cl-sdl-mixer to play the alarm song. If you use ubuntu just install them from the repository using the synaptic package manager.

Here goes the code:

;; Timer

;; Function to return the current time hour and minute in a list
(defun get-time()
(multiple-value-bind (x y z) (get-decoded-time)
(values (list z y) x)))

;; Function to compare if two times are the same
(defun check-time (time-value)
(if (equalp time-value (get-time)) t nil))

;; Function to check if time1 is before time2
(defun before-time-p (time1 time2)
(if (or (< (first time1) (first time2))
(and (equal (first time1) (first time2))
(< (second time1) (second time2))))
nil t))

;; Function to play the alarm song and display the window
(defun play-alarm (song-path)
(sdl:init (logior sdl:+init-audio+ sdl:+init-video+))
(sdl:set-video-mode 320 240 16 sdl:+resizable+)
(sdl:wm-set-caption "Alarm ON!" nil)
(sdl-mix:open-audio 44100 sdl:+audio-s16lsb+ 2 2048)
(let ((music (sdl-mix:load-mus song-path)))
(sdl-mix:play-music music 0)
(sdl:event-loop
(:key-down (key)
(when (= key (char-code #\q))
(progn
(sdl-mix:halt-music)
(sdl-mix:close-audio)
(sdl:quit)
(return))))
(:quit ()
(progn
(sdl-mix:halt-music)
(sdl-mix:close-audio)
(sdl:quit)
(return))))))

;; Function to start the alarm
(defun start-alarm (time-value song-path)
(if (not (before-time-p time-value (get-time)))
nil
(progn
(loop for x from 1
until (check-time time-value)
do (progn (print (get-time)) (sleep 10)))
(format t "~%buzzzzzz... it's ~a:~a~%"
(first time-value)
(second time-value))
(play-alarm song-path))))


; sample usage
;(asdf:oos 'asdf:load-op 'sdl-mix)
;(check-time '(9 38))
;(before-time-p '(12 7) '(12 12))
;(play-alarm "alarm.mp3")
;(start-alarm '(12 15) "alarm.mp3")

Tuesday, July 21, 2009

Setting up SBCL and Emacs on Ubuntu 9.04

I am always forgetting how to do this and every now and then I am finding myself installing LISP somewhere so here it goes my how-to so I don't have to google for it anymore... Nothing wrong with googling something though.

1) sudo apt-get install emacs22-gtk sbcl slime

2) add the following lines to .emacs on home folder.

(setq inferior-lisp-program "/usr/bin/sbcl")
(add-to-list 'load-path "/usr/share/common-lisp/source/slime/")
(require 'slime)
(slime-setup)

Done.

To test your installation:

1) Open emacs: (Applications -> Accessories -> Emacs 22 (GTK)
2) Press ALT + X
3) type slime
SBCL will start you should get a prompt like this:
; SLIME 2010-01-19
CL-USER> 
4) On the prompt type "Hello, World."
You should see this:

; SLIME 2010-01-19
CL-USER> "Hello, World."
"Hello, World."
CL-USER>
5) Congratulations! You have just ran the first obligatory application on any language, but in Lisp!
6) To quit SLIME+LISP: press , then type quit
7) To quit emacs: CTRL+X CTRL+C

Tuesday, May 19, 2009

Outside-in

I am trying to read OnLisp by Paul Graham again and I really liked the turn your imperative code outside-in in chapter 3.

This is almost a reminder to myself.

Let's say I have an imperative function:
(defun imp-fn (x)
(let ((a 2) result)
(setf result (+ x a))
result)

I have seen a lot of code like that.

Turning it outside-in:
1) It returns result what is result?
we can replace it by the expression: (+ x a)
2) what is a?
we can replace it by its expression: (+ x 2)
3) then the result is:
(defun not-so-imp (x)
(+ x 2))

This is a very simple example but the idea remains the same. The OnLisp book has another example which is better than this one, but this one is good enough to show off the idea.