Scheme

I’ve been playing around with scheme a lot in the past couple of days and it’s gotten me thinking about design strategies and how to use less code to get things done. Although I haven’t been doing much interesting stuff with scheme since I re-started using it, I have been reading about some of the more complex functionality of the language, and I hope to start writing some productive code pretty soon. Already I’m noticing tons of things that can be simplified through the use of scheme, and hopefully I can take advantage of this with my projects. I’m not gonna explain what scheme is so if you’re interested visit the link above.

My introduction to scheme came about a year ago, in an introductory programming class that was taught entirely in scheme. At first I hated the language because it was so different from Java and everything else I knew (most of the other people in the class felt the same way), but the language really grew on me as I realized how elegant it is. The course didn’t cover all of the amazing things about the language (i.e. macros, I/O, and using scheme as a scripting language) but we did hit some of them like higher-order procedures, scoping, and different types of recursion. Learning these concepts last year made it a lot easier for me to go back to scheme and pick things up where I left off, so that I can now focus on the stuff I haven’t already learned.

So why is scheme so amazing? First off, higher-order procedures are awesome. I know other languages have them (python), but they’re still really cool and very useful. There are so many situations where I’d want to pass in procedures as arguments, or return procedures from some other procedure and a lot of modern scripting languages are really restricting because they don’t allow you to do that. Secondly, everything can be written in very few lines of code, so that you’re programs stay readable. Most functions that I’ve written don’t exceed 20 lines, and readability is key on larger projects where you’re going to come back and modify stuff. Functions stay small because recursion is the norm in the language. Rather than iterate through things with a looping construct, I’d just use recursion and save a couple of lines of code. Finally, macros. These guys are pretty amazing. They let me write/modify my code at runtime. They let me create special forms and new syntactical structures that I can use later on (like if I need to use a certain structure several times I’d just write a macro for it to keep my code simple). Macros let me customize the language so that it’s perfect for whatever project I need it for. With all of these cool and very useful features, it’s amazing that so few people actually use scheme (or Lisp for that matter) on a daily basis.

There are a couple of ways I would use scheme on my current projects that I think would make my life a lot easier. One definite way would be to interface with mysql and really cut down the number of functions I have that query my database. I’m thinking I would have only 2 functions that generate Insert and Select statements (or one for each type of statement) and then one function that gets the statement and then executes it. Then I can have some other functions that modify/manipulate the mysql results the way I want them to. What’s cool is that I can pass in all the manipulator functions to the general mysql-interaction function so that all of the handling of mysql resources occurs in the same place. Then what I can do is build a macro that performs the mysql command so that I don’t have to make all these function calls every time I want to query mysql; I’ll just have to write one word. This idea hasn’t been fully thought out, nor have I even started to implement it, and it may very well be as lengthy as writing tons of mysql interaction functions, but it seems like it would make interfacing with mysql a lot easier.

Using macros would also be really helpful when getting data from forms vie POST or GET. I tend to store all that data in temp variables, and it happens on pretty much every page of my website. So rather than writing very similar code on each page, I could just make a macro that does that for me and call it on every page. I could have a macro that just takes a hash, and stores all the data in properly scoped variables for the remainder of that page. I hope this idea ends up working because I don’t like to have a lot of scripting code on my content-pages (I believe they should be mostly HTML) and this would take like 5-10 lines off of those pages and move them somewhere else on the back-end.

Unfortunately, there are some problems with scheme. First off, it’s very hard to find a hosting company that’ll have a scheme interpreter for your use, so you won’t be able to use it for web scripting unless you build/maintain your own server(s). I also don’t know how (if at all possible) to interface with mysql, which could threaten the feasibility of the first idea. Finally it may not be easy to embed in HTML (a real plus for PHP), which may be a turn off to a lot of people. I don’t really know if the pro’s outweigh the cons with scheme, but I hope to play around with it and come a conclusion about it in the near future.

Scheme’s obviously a really cool language, but that fact that it doesn’t interface well with other things (mysql, html, other web stuff) gives me doubt to it’s usability. I was thinking that I could use it as a meta-back-end, making it spit out PHP code that I then evaluate at runtime, but this may not work out as planned if I can’t interface scheme with PHP. Even just by itself, scheme is really powerful and can do a lot, but as I’m trying to build a website, it’s not much use to me on it’s own. I’m gonna be spending the next couple of days trying to get scheme to interface with other stuff, and after that, I’ll have a better feeling for how useful scheme will actually be to me.

Tags: , ,

Leave a Reply