Friday, August 8, 2008

AI Scheme

Just for fun, a few days ago I wrote a minimax AI algorithm in Scheme. This was mainly a way to become more familiar with writing in Scheme, and so I wrote it from scratch. I had written pieces of Scheme a year ago but wanted to write something more practical. Writing in a functional language is fun because it pushes you to see problems from a new perspective. (For those unfamiliar with the concept, of the code I wrote there are no variable assignments, for loops, while loops, or C-like arrays). Also, when finished, I was able to format the code nicely.



To use this algorithm, you can provide definitions for create_new_table,get_open_spaces,place_at_position, and is_winning, and that will be all you need. This zip file contains the complete program and an example game implementation.

If I get tired of S-expression syntax, JavaScript is actually a good language for functional programming. It has the ability to do concisely express things like function() { call_something(); return function(){ do_something(); } }(); or function adder(x) { return function(y) {print('!'); return x+y}; } f = adder(1); f(1);. In fact, it did not take very much work for all of the The Little Schemer examples to be written in JavaScript; see Douglas Crockford's page and this. Python's lambda, on the other hand, is intentionally limited to containing one expression, and there not seem to be any convenient way to create an anonymous inner function with scope.

1 comment:

arctica2 said...

!! Hey Ben, I love your blog...I stumbled across it looking for docs on the Marauder's Map. I'm in awe of the amount of code you have written! Very cool. Next time I see you in the dining hall maybe I'll come talk to you about some of these projects, they're awesome:-)

~Charlie Offenbacher (2012)