<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,6 @@
 class Function
-  def initialize(formals = [], body = nil, &amp;block)
+  def initialize(scope, formals = [], body = nil, &amp;block)
+    @scope = scope
     @formals = formals.map { |f| f.to_s }
     @body = body || block
   end
@@ -7,8 +8,9 @@ class Function
   def call(scope, args)
     args = args.map { |a| a.eval(scope) }
     return @body.call(*args) if primitive?
-    @formals.each_with_index { |name, i| scope[name] = args[i] }
-    @body.map { |expr| expr.eval(scope) }.last
+    closure = Scope.new(@scope)
+    @formals.each_with_index { |name, i| closure[name] = args[i] }
+    @body.map { |expr| expr.eval(closure) }.last
   end
   
   def primitive?</diff>
      <filename>lib/function.rb</filename>
    </modified>
    <modified>
      <diff>@@ -15,11 +15,11 @@ class Scope
   end
   
   def define(name, *args, &amp;block)
-    self[name] = Function.new(*args, &amp;block)
+    self[name] = Function.new(self, *args, &amp;block)
   end
   
   def syntax(name, &amp;block)
-    self[name] = Syntax.new(&amp;block)
+    self[name] = Syntax.new(self, &amp;block)
   end
 end
 
@@ -28,8 +28,19 @@ class TopLevel &lt; Scope
     super
     
     syntax('define') do |scope, cells|
+      first = cells.first
+      case first
+        when Lisp::List
+          names = first.cells.map { |c| c.text_value }
+          scope.define(names.first, names[1..-1], cells[1..-1])
+        when Lisp::Identifier
+          scope[first.text_value] = cells[1].eval(scope)
+      end
+    end
+    
+    syntax('lambda') do |scope, cells|
       names = cells.first.cells.map { |c| c.text_value }
-      scope.define(names.first, names[1..-1], cells[1..-1])
+      Function.new(scope, names, cells[1..-1])
     end
     
     syntax('if') do |scope, cells|</diff>
      <filename>lib/scope.rb</filename>
    </modified>
    <modified>
      <diff>@@ -3,5 +3,15 @@
       1
       (* x (fact (- x 1)))))
 
-(fact 6)
+(display (fact 6))
+
+(define (add x)
+  (lambda (y)
+    (+ x y)))
+
+(define add4 (add 4))
+(define add5 (add 5))
+
+(display (add4 3))
+(display (add5 3))
 </diff>
      <filename>test.scm</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>19957288a65041dd5edd59c74824dcc7329ea485</id>
    </parent>
  </parents>
  <author>
    <name>James Coglan</name>
    <email>jcoglan@googlemail.com</email>
  </author>
  <url>http://github.com/jcoglan/stickup/commit/68589dd7595fe9d59b6d4d07e07199eec4e0ab93</url>
  <id>68589dd7595fe9d59b6d4d07e07199eec4e0ab93</id>
  <committed-date>2009-03-29T09:17:30-07:00</committed-date>
  <authored-date>2009-03-29T09:17:30-07:00</authored-date>
  <message>Adding closures by making each function remember its defining scope.</message>
  <tree>1d4159623a37731e0a45838e31779d29e416cfa7</tree>
  <committer>
    <name>James Coglan</name>
    <email>jcoglan@googlemail.com</email>
  </committer>
</commit>
