Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'object'
- def for_each(list)
- loop do
- yield(list.car)
- list = list.cdr
- return if list.nil?
- end
- end
- def map(list)
- [].tap do |r|
- loop do
- r << yield(list.car)
- list = list.cdr
- return r if list.nil?
- end
- end
- end
- class Evalator
- def initialize env
- @env = env
- end
- def myeval(expr)
- if expr.respond_to?(:car)
- fn = expr.car
- args = map(expr.cdr){|e| myeval(e)}
- find(fn).call(*args)
- else
- expr
- end
- end
- def find(sym)
- return @env[sym]
- end
- end
- if __FILE__ == $0
- e = Evalator.new({:+ => lambda{|*args| args.inject(0){|s,n| s + n}}})
- Cons::list(:+, 1, 2)
- # for_each(Cons::list(:+, 1, 2)){|e| p e}
- # p map(Cons::list(:+, 1, 2)){|e| e}
- p e.myeval(Cons::list(:+, 1, 2))
- end
Add Comment
Please, Sign In to add comment