Tuesday, March 10, 2009

Found Something, maybe

For a long time I've been searching for a language to replace C/C++ for the majority of my development but I've never stuck to one language for every long. There was always something I would find out about the language after messing around with it for a bit that I knew would get in my way later on in development. Personally I was looking for something that compiled to native code for speed, low memory overhead, portability to embedded devices and most of all easy compatibility with C so I can use good libraries/APIs like OpenGL and OpenAL or rewrite some of the slow parts of my code in C if needed. OCaml almost fit this bill but I just couldn't get over the weird syntax and I keep running into issues with it on OS X.

Now, while I have used scheme before, mostly while I was at the university (PLT/ Dr Scheme), I always thought of it as a learning language and nobody used it or could use it for real world coding. The more I dug into finding a new language, scheme kept popping up every now and then so I decided to take a closer look. To my surprise there were quite a few other implementations I didn't know about, of those were Gambit-C and Chicken Scheme that stood out to me. The main drawing factor was that they both compiled to C, could call native C easily and could be called from C.

I first started with Gambit-C as I found benchmarks that said it was the fastest one of the two, I don't know if those benchmarks were biased or still held true for the current releases but I figured it was worth a shot. I started messing around with Gambit-C and it was quite fun but when I decided that I wanted to give some game programming a shot I noticed that there were very few libraries and support in general. I decided to investigate Chicken Scheme to see if it was any better, I found out it has its own package manager with tons of "eggs" (what they call a package). Of those packages people had already created interfaces for SDL, OpenGL, OpenAL and a ton more of things I found useful. Needless to say I switched over to Chicken Scheme, it seemed to have more support and at the very least I could use all the existing packages.

The one thing about scheme that somewhat bothers me right now is the syntax. There are tons of parenthesis but I understand why because of S-expressions and I think I will learn to tolerate it (after all I did get use to OCaml). I'm tired of language hopping for now so I decided that I will at the very least write a game or two in it and see where it takes me. I've also decided that I want to semi-document things. I haven't decided what exactly that means but at the very least I plan on throwing together some tutorial-esque posts with some source code to go along with it. I'll say this right now however, I know I'm not a good scheme/functional programmer (yet) so if you see something weird or I'm over complicating things, I probably am (feel free to point it out to me).

Setting up your Environment

I've decided to use Ubuntu Linux for development. My main reason are:
  • Package management is easier than windows for development.
  • The windows command line sucks and many scheme implementations use the command line for normal tasks.
  • My vista installation doesn't like OpenGL (yes I have the latest amd/ati drivers)
  • I miss alt-f2 to run a program by name

In other words, personal preference :). If your following along on windows I highly suggest you either get cygwin or msys and friends up and running on your system, it will make things a lot easier.

Installing Chicken Scheme

The latest version in 8.10 is 2.5-1 which is extremely old, the current stable release as of right now is 3.4 . The semi-good news is the next version of Ubuntu will have a more updated version 3.2.7-2 which is more acceptable but not ideal. I installed these packages on 8.10 without any issues. You can grab all the files needed through launchpad (just get the .deb files on these pages:

(for amd64 processors x86-64)
amd64 - libchicken3/3.2.7-2
amd64 - libchicken-dev/3.2.7-2
amd64 - chicken-bin/3.2.7-2

(for i386 processors x86)
i386 - libchicken3/3.2.7-2
i386 - libchicken-dev/3.2.7-2
i386 - chicken-bin/3.2.7-2

Make sure to install them in the order listed, if you don't then it will complain about a missing dependency.

Next feel free to try out chicken scheme, I made a simple little fib program (seems to be the "standard" example for lisps).

(define (fib x)
(define (fib-helper x y)
(if (= x 0)
(fib-helper (- x 1) (* x y))))
(fib-helper x 1))

(print (fib 5))

Save off to a file named something like "schemeTest.scm". Now open up the command line and navigate to where you saved the file and type "csi schemeTest.scm" or whatever you named the file. You should see it print out something like this:

sean@sean-linux:~/programming$ csi schemeTest.scm

(c)2008 The Chicken Team
(c)2000-2007 Felix L. Winkelmann
Version 3.2.7 - linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables applyhook hostpcre ]
compiled 2008-11-05 on debian (Linux)

; loading schemeTest.scm ...

Pressing ctrl-d will exit out of the interactive console.

Hopefully everything will be working now. Next I want to install SDL and OpenGL for game development. Through synaptic/the package manager install libSDL-dev libSDL_image-dev libSDL_net-dev libSDL_gfx-dev libSDL_mixer-dev . Once those are done installing open up a command line and login as root ("sudo su"). type "chicken-setup sdl" then "chicken-setup opengl". If you get errors then usually it will say something to the effect of "couldn't find header file 'sdl.h' ", that means you should search in synaptic and install the -dev version of the file needed (however you shouldn't run into any errors).

The last thing that I recommend is a decent editor for your source code. There are a few choices out there, emacs seems to be the preferred environment however I don't feel like learning all the commands of emacs and I want more of UI based editor. I decided to go with scite, it's quite simple but it has all I need, syntax highlighting, code folding, parenthesis highlighter and auto indentation. Once you have everything setup you should be good to go for development.

While I don't plan to teach scheme, I do think it's a good idea to show some example code and hopefully people can follow along. If you don't know scheme at all I recommend visiting here and get a brief overview. Another good resource is http://mitpress.mit.edu/sicp/ , it is an online book with exercises to test your knowledge.

On a closing note, I doubt that I will be posting back to back like this again. The main reason for the fast post is because I had this post in mind when starting my blog. The last post was mainly just an introduction and a repost of something I had written previously that related to this post. I hope to keep this updated somewhat regularly but I'm making no promises.

No comments:

Post a Comment