Skip to content

Instantly share code, notes, and snippets.

@siwka siwka/lisp_ruby.rb
Created Sep 25, 2014

Embed
What would you like to do?
def lisp_eval(x)
if x == '1'
1
elsif x == '#t'
true
else
list = x.gsub(" ", '').chars
parser(list)
end
end
def parser(list)
return list.first.to_i unless list.join('').match(/\(.*\)/)
sub_list = list[1..-2]
if sub_list.join('').match(/\(.*\)/)
start_idx = sub_list.index('(')
end_idx = sub_list.length - sub_list.reverse.index(')')
val = parser(sub_list[start_idx..end_idx])
before = sub_list[0..start_idx]
after = sub_list[end_idx..sub_list.length]
sub_list = (before << val) + after
end
operator = sub_list.shift
sub_list.reduce { |x,y| x.to_i.send(operator, y.to_i)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.